caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* obj.magic for polymorphic record fields
@ 2009-12-20 17:44 Jacques Le Normand
  2009-12-20 18:17 ` [Caml-list] " Nicolas Pouillard
  0 siblings, 1 reply; 2+ messages in thread
From: Jacques Le Normand @ 2009-12-20 17:44 UTC (permalink / raw)
  To: caml-list caml-list

[-- Attachment #1: Type: text/plain, Size: 341 bytes --]

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?


Jacques

[-- Attachment #2: Type: text/html, Size: 603 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Caml-list] obj.magic for polymorphic record fields
  2009-12-20 17:44 obj.magic for polymorphic record fields Jacques Le Normand
@ 2009-12-20 18:17 ` Nicolas Pouillard
  0 siblings, 0 replies; 2+ messages in thread
From: Nicolas Pouillard @ 2009-12-20 18:17 UTC (permalink / raw)
  To: Jacques Le Normand; +Cc: caml-list caml-list

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-12-20 18:17 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-20 17:44 obj.magic for polymorphic record fields Jacques Le Normand
2009-12-20 18:17 ` [Caml-list] " Nicolas Pouillard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).