From: Nicolas Pouillard <nicolas.pouillard@gmail.com>
To: Jacques Le Normand <rathereasy@gmail.com>
Cc: caml-list caml-list <caml-list@yquem.inria.fr>
Subject: Re: [Caml-list] obj.magic for polymorphic record fields
Date: Sun, 20 Dec 2009 19:17:27 +0100 [thread overview]
Message-ID: <1261332875-sup-5067@peray> (raw)
In-Reply-To: <f74178430912200944w28145e73vebbe43912fcc5ea4@mail.gmail.com>
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
next prev parent reply other threads:[~2009-12-20 18:17 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-12-20 17:44 Jacques Le Normand
2009-12-20 18:17 ` Nicolas Pouillard [this message]
2009-12-20 18:45 Re[2]: [Caml-list] " Damien Guichard
2009-12-21 13:44 ` Goswin von Brederlow
2009-12-21 14:00 ` Boris Yakobowski
2009-12-21 16:05 ` Jacques Le Normand
2009-12-22 13:35 ` Goswin von Brederlow
2009-12-22 21:49 ` Boris Yakobowski
2009-12-24 12:10 ` Goswin von Brederlow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1261332875-sup-5067@peray \
--to=nicolas.pouillard@gmail.com \
--cc=caml-list@yquem.inria.fr \
--cc=rathereasy@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox