English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    
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: bluestorm <bluestorm.dylc@g...>
Subject: Re: [Caml-list] extending records with Obj.magic
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)