caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Bruno Barras <bruno.barras@inria.fr>
To: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] extending records with Obj.magic
Date: Thu, 02 Sep 2010 15:06:41 +0200	[thread overview]
Message-ID: <4C7FA161.1060705@inria.fr> (raw)
In-Reply-To: <231FBA54-C413-4696-AA70-216253EDF2DA@math.harvard.edu>

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.


      parent reply	other threads:[~2010-09-02 13:06 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20100830204956.2D927BC5D@yquem.inria.fr>
2010-08-30 21:04 ` 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 message]

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=4C7FA161.1060705@inria.fr \
    --to=bruno.barras@inria.fr \
    --cc=caml-list@yquem.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;
as well as URLs for NNTP newsgroup(s).