Browse thread
extending records with Obj.magic
-
Nicolas Ojeda Bar
- Bruno Barras
- bluestorm
- Martin Jambon
[
Home
]
[ Index:
by date
|
by threads
]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
| Date: | -- (:) |
| From: | Bruno Barras <bruno.barras@i...> |
| Subject: | Re: [Caml-list] extending records with Obj.magic |
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.