Version française
Home     About     Download     Resources     Contact us    
Browse thread
extending records with Obj.magic
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Martin Jambon <martin.jambon@e...>
Subject: Re: [Caml-list] extending records with Obj.magic
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/