* extending records with Obj.magic
[not found] <20100830204956.2D927BC5D@yquem.inria.fr>
@ 2010-08-30 21:04 ` Nicolas Ojeda Bar
2010-08-30 21:11 ` [Caml-list] " bluestorm
` (2 more replies)
0 siblings, 3 replies; 5+ messages in thread
From: Nicolas Ojeda Bar @ 2010-08-30 21:04 UTC (permalink / raw)
To: caml-list
Hello,
I need extensible records, and the code below seems to
work. Are there any pitfall that I should be aware of?
Could this mess up the GC?
# type t0 = { a : int };
# type t1 = { a2 : int; b : int };
# value x1 = { a2 = 3; b = 5 };
# value x0 : t0 = Obj.magic x1;
value x0 = { a = 3 }
# value x0' : t1 = Obj.magic x0;
value x0' = { a2 = 3; b = 5 }
(supposedly t1 is an extension of t0). The types are
being generated by a program, so I am not worried about
actually having to _write_ this myself.
Thanks!
N
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] extending records with Obj.magic
2010-08-30 21:04 ` extending records with Obj.magic Nicolas Ojeda Bar
@ 2010-08-30 21:11 ` bluestorm
2010-08-30 21:43 ` Martin Jambon
2010-09-02 13:06 ` Bruno Barras
2 siblings, 0 replies; 5+ messages in thread
From: bluestorm @ 2010-08-30 21:11 UTC (permalink / raw)
To: Nicolas Ojeda Bar; +Cc: caml-list
On Mon, Aug 30, 2010 at 11:04 PM, Nicolas Ojeda Bar
<nojb@math.harvard.edu> wrote:
> Hello,
>
> I need extensible records, and the code below seems to
> work. Are there any pitfall that I should be aware of?
> Could this mess up the GC?
>
> # type t0 = { a : int };
> # type t1 = { a2 : int; b : int };
> # value x1 = { a2 = 3; b = 5 };
> # value x0 : t0 = Obj.magic x1;
> value x0 = { a = 3 }
> # value x0' : t1 = Obj.magic x0;
> value x0' = { a2 = 3; b = 5 }
>
You could use objects instead.
ype t0 = < a : int >
type t1 = < a : int; b : int >
let x1 = object
method a = 3
method b = 5
end
let x0 = (x1 :> t0)
(downcasting x0 -> x0' isn't correct)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] extending records with Obj.magic
2010-08-30 21:04 ` extending records with Obj.magic Nicolas Ojeda Bar
2010-08-30 21:11 ` [Caml-list] " bluestorm
@ 2010-08-30 21:43 ` Martin Jambon
2010-09-06 15:24 ` Goswin von Brederlow
2010-09-02 13:06 ` Bruno Barras
2 siblings, 1 reply; 5+ messages in thread
From: Martin Jambon @ 2010-08-30 21:43 UTC (permalink / raw)
To: Nicolas Ojeda Bar; +Cc: caml-list
Nicolas Ojeda Bar wrote:
> Hello,
>
> I need extensible records, and the code below seems to
> work. Are there any pitfall that I should be aware of?
> Could this mess up the GC?
>
> # type t0 = { a : int };
> # type t1 = { a2 : int; b : int };
> # value x1 = { a2 = 3; b = 5 };
> # value x0 : t0 = Obj.magic x1;
> value x0 = { a = 3 }
> # value x0' : t1 = Obj.magic x0;
> value x0' = { a2 = 3; b = 5 }
>
> (supposedly t1 is an extension of t0). The types are
> being generated by a program, so I am not worried about
> actually having to _write_ this myself.
# type t0 = { a : float };;
type t0 = { a : float; }
# type t1 = { a2 : float; b : int };;
type t1 = { a2 : float; b : int; }
# let x1 = { a2 = 3.; b = 5 };;
val x1 : t1 = {a2 = 3.; b = 5}
# let x0 : t0 = Obj.magic x1;;
val x0 : t0 = {a = 3.}
And now the magic:
# x0.a;;
- : float = 6.94364726476075e-310
Martin
--
http://mjambon.com/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] extending records with Obj.magic
2010-08-30 21:04 ` extending records with Obj.magic Nicolas Ojeda Bar
2010-08-30 21:11 ` [Caml-list] " bluestorm
2010-08-30 21:43 ` Martin Jambon
@ 2010-09-02 13:06 ` Bruno Barras
2 siblings, 0 replies; 5+ messages in thread
From: Bruno Barras @ 2010-09-02 13:06 UTC (permalink / raw)
To: caml-list
On 08/30/2010 11:04 PM, Nicolas Ojeda Bar wrote:
> Hello,
>
> I need extensible records, and the code below seems to
> work. Are there any pitfall that I should be aware of?
> Could this mess up the GC?
>
> # type t0 = { a : int };
> # type t1 = { a2 : int; b : int };
> # value x1 = { a2 = 3; b = 5 };
> # value x0 : t0 = Obj.magic x1;
> value x0 = { a = 3 }
> # value x0' : t1 = Obj.magic x0;
> value x0' = { a2 = 3; b = 5 }
You should also be aware that structural equality (=) is untyped, so you
might get surprised by this:
type t0 = { a : int; };;
# type t1 = { a2 : int; b : int; }
type t1 = { a2 : int; b : int; };;
# let x = {a2=3; b=1};;
val x : t1 = {a2 = 3; b = 1}
# let y = {a2=3; b=2};;
val y : t1 = {a2 = 3; b = 2}
# let x0 : t0 = Obj.magic x;;
val x0 : t0 = {a = 3}
# let y0 : t0 = Obj.magic y;;
val y0 : t0 = {a = 3}
# x0 = y0;;
- : bool = false
x0 and y0 look structurally equal, but they're not!
The same oddity will show up with Pervasives.compare and
Pervasives.hash, just to mention those two.
Bruno.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Caml-list] extending records with Obj.magic
2010-08-30 21:43 ` Martin Jambon
@ 2010-09-06 15:24 ` Goswin von Brederlow
0 siblings, 0 replies; 5+ messages in thread
From: Goswin von Brederlow @ 2010-09-06 15:24 UTC (permalink / raw)
To: caml-list
Martin Jambon <martin.jambon@ens-lyon.org> writes:
> Nicolas Ojeda Bar wrote:
>> Hello,
>>
>> I need extensible records, and the code below seems to
>> work. Are there any pitfall that I should be aware of?
>> Could this mess up the GC?
>>
>> # type t0 = { a : int };
>> # type t1 = { a2 : int; b : int };
>> # value x1 = { a2 = 3; b = 5 };
>> # value x0 : t0 = Obj.magic x1;
>> value x0 = { a = 3 }
>> # value x0' : t1 = Obj.magic x0;
>> value x0' = { a2 = 3; b = 5 }
>>
>> (supposedly t1 is an extension of t0). The types are
>> being generated by a program, so I am not worried about
>> actually having to _write_ this myself.
You need to make sure t1 is structurally an extension of t0 and then it
will be safe to use in the current implementation of the GC (see counter
example below). As others have said beware of functions that do
structural compares on the magiced values. But GC compaction will keep
the value intact as it only looks at the in-memory structure and
Obj.magic doesn't change that.
> # type t0 = { a : float };;
> type t0 = { a : float; }
> # type t1 = { a2 : float; b : int };;
> type t1 = { a2 : float; b : int; }
> # let x1 = { a2 = 3.; b = 5 };;
> val x1 : t1 = {a2 = 3.; b = 5}
> # let x0 : t0 = Obj.magic x1;;
> val x0 : t0 = {a = 3.}
>
> And now the magic:
>
> # x0.a;;
> - : float = 6.94364726476075e-310
This is a special case because records with ONLY floats are stored as
unboxed float array. Mixed records on the other hand use boxed
floats. So in you example t0 is structurally not a prefix of t1.
MfG
Goswin
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2010-09-06 15:24 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20100830204956.2D927BC5D@yquem.inria.fr>
2010-08-30 21:04 ` extending records with Obj.magic Nicolas Ojeda Bar
2010-08-30 21:11 ` [Caml-list] " bluestorm
2010-08-30 21:43 ` Martin Jambon
2010-09-06 15:24 ` Goswin von Brederlow
2010-09-02 13:06 ` Bruno Barras
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox