From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Original-To: caml-list@yquem.inria.fr Delivered-To: caml-list@yquem.inria.fr Received: from mail3-relais-sop.national.inria.fr (mail3-relais-sop.national.inria.fr [192.134.164.104]) by yquem.inria.fr (Postfix) with ESMTP id B9089BC57 for ; Thu, 2 Sep 2010 15:06:30 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AnEBAI8+f0yBaB4OmWdsb2JhbACTV40pFQEBAQEBCAsKBxEivTCFOwSKGQ X-IronPort-AV: E=Sophos;i="4.56,308,1280700000"; d="scan'208";a="56567979" Received: from mx-a.polytechnique.fr ([129.104.30.14]) by mail3-smtp-sop.national.inria.fr with ESMTP; 02 Sep 2010 15:06:30 +0200 Received: from argos.lix.polytechnique.fr (argos.lix.polytechnique.fr [129.104.11.2]) by mx-a.polytechnique.fr (tbp 5.3.2/2.0.7) with ESMTP id o82D6Twu019012 for ; Thu, 2 Sep 2010 15:06:29 +0200 Received: from [129.104.11.145] (ausone.polytechnique.fr [129.104.11.145]) by argos.lix.polytechnique.fr (Postfix) with ESMTP id 2BF04AAC82 for ; Thu, 2 Sep 2010 15:06:29 +0200 (CEST) Message-ID: <4C7FA161.1060705@inria.fr> Date: Thu, 02 Sep 2010 15:06:41 +0200 From: Bruno Barras User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.11) Gecko/20100713 Thunderbird/3.0.6 MIME-Version: 1.0 To: caml-list@yquem.inria.fr Subject: Re: [Caml-list] extending records with Obj.magic References: <20100830204956.2D927BC5D@yquem.inria.fr> <231FBA54-C413-4696-AA70-216253EDF2DA@math.harvard.edu> In-Reply-To: <231FBA54-C413-4696-AA70-216253EDF2DA@math.harvard.edu> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Spam: no; 0.00; untyped:01 val:01 val:01 bool:01 structurally:01 pervasives:01 pervasives:01 hash:01 equality:01 wrote:01 extensible:01 caml-list:01 int:01 int:01 seems:03 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.