Version française
Home     About     Download     Resources     Contact us    
Browse thread
function specialization
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jørgen Hermanrud Fjeld <jhf@h...>
Subject: Re: [Caml-list] function specialization
From the top of my head, with objects (and classes) you can create 
a method that is polymorphic for each invocation "http://caml.inria.fr/pub/docs/manual-ocaml/manual005.html#ss:polymorphic-methods".
For instance:
(**)
class type wrapper = object method wrap : 'a.('a->unit)->'a->unit end;;

let wrapper:wrapper = object 
    method wrap : 'a.('a->unit)->'a->unit = 
	fun fn arg -> fn arg 
end ;;


let fn1 (v: int)    = ()
let fn2 (v: string) = ()

let use = wrapper#wrap fn1 1 ; wrapper#wrap fn2 "" ;;
let use (wrapper:wrapper) = wrapper#wrap fn1 1; wrapper#wrap fn2 "" ;;

use wrapper ;;
(**)


On Tue, Nov 15, 2005 at 02:45:40PM +0300, Vsevolod Fedorov wrote:
> Hello!
> 
> I have code like following:
> ----
> 
> type 'a wrap = ('a -> unit) -> 'a -> unit
> 
> let wrap fn arg =
>   fn arg
> 
> let fn1 (v: int)    = ()
> let fn2 (v: string) = ()
> 
> let use (wrap: 'a wrap) =
>   wrap fn1 1;
>   wrap fn2 ""
>   (*   ^^^ This expression has type string -> unit but is here used with
> type int -> unit *)
> 
> ------
> and get this error from compiler.
> Is there any way to use 'wrap' function for both cases: fn1 and fn2?
> Or is there some 'standard' workarounds/solutions for this problem exists?
> 
> Thanks
> Seva
> 
> 
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
> 

-- 
Sincerely | Homepage:
Jørgen    | http://www.hex.no/jhf
          | Public GPG key:
          | http://www.hex.no/jhf/key.txt

Truly great madness can not be achieved without significant intelligence.
		-- Henrik Tikkanen