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 AD6ADBC57 for ; Mon, 6 Sep 2010 17:24:33 +0200 (CEST) X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: AuUBADOkhEzZSMDqkWdsb2JhbACTSI0qFQEBAQEJCwoHEQMfvFSFPQQ X-IronPort-AV: E=Sophos;i="4.56,325,1280700000"; d="scan'208";a="56749010" Received: from fmmailgate03.web.de ([217.72.192.234]) by mail3-smtp-sop.national.inria.fr with ESMTP; 06 Sep 2010 17:24:33 +0200 Received: from smtp01.web.de ( [172.20.0.243]) by fmmailgate03.web.de (Postfix) with ESMTP id 76C9915EEB655 for ; Mon, 6 Sep 2010 17:24:32 +0200 (CEST) Received: from [78.43.204.177] (helo=frosties.localdomain) by smtp01.web.de with asmtp (TLSv1:AES256-SHA:256) (WEB.DE 4.110 #24) id 1OsdYe-0000eI-00 for caml-list@yquem.inria.fr; Mon, 06 Sep 2010 17:24:32 +0200 Received: from mrvn by frosties.localdomain with local (Exim 4.72) (envelope-from ) id 1OsdYd-0006TM-VX for caml-list@yquem.inria.fr; Mon, 06 Sep 2010 17:24:32 +0200 From: Goswin von Brederlow 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> <4C7C2603.9080909@ens-lyon.org> Date: Mon, 06 Sep 2010 17:24:31 +0200 In-Reply-To: <4C7C2603.9080909@ens-lyon.org> (Martin Jambon's message of "Mon, 30 Aug 2010 14:43:31 -0700") Message-ID: <87eid6yjww.fsf@frosties.localdomain> User-Agent: Gnus/5.110009 (No Gnus v0.9) XEmacs/21.4.22 (linux, no MULE) MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Sender: goswin-v-b@web.de X-Sender: goswin-v-b@web.de X-Provags-ID: V01U2FsdGVkX18szcwrAxpPMFzrsY3pLzA9/N11S0G2/4GGJ6Tc NsKVVaIQiKFXsMEWqztsUz0GTQ9itzCpgW4N09csSXsJ/KJO1v U0oE43xio= X-Spam: no; 0.00; ens-lyon:01 structurally:01 compaction:01 in-memory:01 val:01 val:01 unboxed:01 structurally:01 mfg:98 wrote:01 extensible:01 caml-list:01 functions:01 compares:01 int:01 Martin Jambon writes: > 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. You need to make sure t1 is structurally an extension of t0 and then it will be safe to use in the current implementation of the GC (see counter example below). As others have said beware of functions that do structural compares on the magiced values. But GC compaction will keep the value intact as it only looks at the in-memory structure and Obj.magic doesn't change that. > # 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 This is a special case because records with ONLY floats are stored as unboxed float array. Mixed records on the other hand use boxed floats. So in you example t0 is structurally not a prefix of t1. MfG Goswin