From: rixed@happyleptic.org
To: caml-list@inria.fr
Subject: [Caml-list] Bless me Father, for I have used Obj.magic
Date: Thu, 22 Dec 2011 22:24:30 +0100 [thread overview]
Message-ID: <20111222212429.GA8156@yeeloong.happyleptic.org> (raw)
I have these types:
type t = { operation : t1 -> t2 ;
some_fields : of_some_types }
type t_priv = { t : t ;
some_other_fields : of_some_other_types }
In other words, I have a "super" type t that's further specialized
by t_priv, which is hidden within t.operation as shown below in the
constructor for t values :
let make ... =
let some_operation t_priv t1 =
... use t_priv to return a t2 ... in
let rec t_priv = { t ;
some_other_fields = some_values }
and t = { operation = some_operation t_priv ;
some_fields = some_values }
in t
This does not compile because of the way t_priv is used within t
construction. The policed way to get around this seams to be:
type t_priv = { mutable t : t option ;
some_other_fields : of_some_other_types }
and then:
let make ... =
...
let t_priv = { t = None ; ... } in
let t = { operation = some_operation t_priv ; ... } in
t_priv.t <- Some t in
t
But then every time one need to use t_priv.t one use to deconstruct the
option (equivalent of what one would do using dark age languages
checking at runtime for proper initialization of a field).
So I ended up doing:
type t_priv = { mutable t : t ; (* look ma! no option! *)
some_other_fields : of_some_other_types }
let make ... =
...
let t_priv = { t = Obj.magic 0 ; (* Frères humains qui après nous codez... *)
... } in
t_priv.t <- { operation = some_operation t_priv ; ... } in
t_priv.t
In my simplistic model of what's happening under the hood, the object
for t_priv will hold for a moment a 0 in its t field instead of a
pointer to a legitimate t object, but that's not a problem even if the
GC awakes right at that moment since it does not use the actual types
(they are gone by then) but the tags, and the 0 is tagged as an integer
so from its point of view all is fine. And apparently the program is
running well.
Should I fear some backfire?
next reply other threads:[~2011-12-22 21:26 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-22 21:24 rixed [this message]
2011-12-22 22:07 ` Fabrice Le Fessant
2011-12-22 22:19 ` rixed
2011-12-22 23:29 ` Thierry Martinez
2011-12-23 12:04 ` rixed
2011-12-23 12:26 ` Fabrice Le Fessant
2011-12-30 11:41 ` Damien Doligez
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=20111222212429.GA8156@yeeloong.happyleptic.org \
--to=rixed@happyleptic.org \
--cc=caml-list@inria.fr \
/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