Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re[2]: [Caml-list] obj.magic for polymorphic record fields
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Damien Guichard <alphablock@o...>
Subject: Re[2]: [Caml-list] obj.magic for polymorphic record fields
> I once faced this situation and the solution is to use modules.
That is one good practical solution.

The simpler solution that immediatly came to my mind is eta-expansion.

  type foo = {bar : 'a. 'a -> 'a}
  let a : int -> int = fun x -> x
  let baz = {bar = fun x -> (Obj.magic a) x}

However it issues a warning so i acknowledge it's less elegant.

- damien


Le 20/12/2009 à 19:17:27, Nicolas Pouillard <nicolas.pouillard@gmail.com> à
écrit :
>
>Excerpts from Jacques Le Normand's message of Sun Dec 20 18:44:57 +0100 2009:
>> Dear ocaml-list,
>> the following code does not type check:
>> 
>> type foo = {bar : 'a. 'a -> 'a}
>> let a : int -> int = fun x -> x
>> let baz = {bar = Obj.magic a}
>> 
>> with the error
>> 
>> Error: This field value has type 'a -> 'a which is less general than
>>          'b. 'b -> 'b
>> 
>> 
>> my question is: how could I use obj.magic to get a polymorphic type?
>
>Apart from the traditional
>  "You should avoid Obj.magic as much as possible!"
>
>I once faced this situation and the solution is to use modules.
>
>module M : sig
>  val f : 'a -> 'a
>end = struct
>  let f = Obj.magic
>end ;;
>let baz = {bar = M.f };;
>
>Have fun!
>
>-- 
>Nicolas Pouillard
>http://nicolaspouillard.fr
>
>_______________________________________________
>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


--
Mail created using EssentialPIM Free - www.essentialpim.com