From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail4-relais-sop.national.inria.fr (mail4-relais-sop.national.inria.fr [192.134.164.105]) by walapai.inria.fr (8.13.6/8.13.6) with ESMTP id q2OIisrV026742 for ; Sat, 24 Mar 2012 19:44:54 +0100 X-IronPort-Anti-Spam-Filtered: true X-IronPort-Anti-Spam-Result: Ao0BAOEUbk/RVdS0imdsb2JhbAAqGrgZCCIBAQEKCQ0HEgYjggkBAQEDARICLAEBNwEECwsLFiUPAQQgAQUBIhMihW+BdAUEBymaXwqKWoQtAY1fBpEolWSBEY09PYQJgVw X-IronPort-AV: E=Sophos;i="4.73,642,1325458800"; d="scan'208";a="137566053" Received: from mail-wi0-f180.google.com ([209.85.212.180]) by mail4-smtp-sop.national.inria.fr with ESMTP/TLS/RC4-SHA; 24 Mar 2012 19:44:49 +0100 Received: by wibhm17 with SMTP id hm17so2882362wib.9 for ; Sat, 24 Mar 2012 11:44:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-type; bh=wAjPeW7orenWkjasijhE+VxvKFXJJdXy21H5ZzWLftU=; b=qlh+URQb4bpePmydVWC8380EMVyxQErcaUZjmkh48/wTdTd5B764pJ4qb7l28IeZfh zZUIRjKu/iR8QPXptGUqA3iD3w3Y3hS7TGjASmZDrvLW+YRvhF5h27Z5+hv/Dnt0kLjP dS84XC1a+eHu2ucpVGTLm/W+8yEf/0ueO9sndtnpJwl38e9P2q+szJc61+MeERF+emBj vyD8M812uUYaVUbdjscd+BeTaqlUbX5aBGMFjGa7yDGtBQVYipKvpNIv/3PmyupABUhk +x4l5gzE9F1eklLE7GHj8s4x3/2u//t9/F5WqbAorZdHuknwNqm+t9LmnMUOaUCQrmkY 0ZGg== Received: by 10.180.107.101 with SMTP id hb5mr6523775wib.7.1332614688767; Sat, 24 Mar 2012 11:44:48 -0700 (PDT) Received: from spec-desktop.danmey.org (cpc1-cmbg12-0-0-cust201.5-4.cable.virginmedia.com. [86.9.116.202]) by mx.google.com with ESMTPS id k7sm41101172wia.5.2012.03.24.11.44.47 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 24 Mar 2012 11:44:48 -0700 (PDT) From: Wojciech Meyer To: Goswin von Brederlow Cc: caml-list@inria.fr References: <87fwcx6ejm.fsf@frosties.localnet> Date: Sat, 24 Mar 2012 18:45:02 +0000 In-Reply-To: <87fwcx6ejm.fsf@frosties.localnet> (Goswin von Brederlow's message of "Sat, 24 Mar 2012 19:26:37 +0100") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.94 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain Subject: Re: [Caml-list] Wish: mutable variant types, equivalence with records Goswin von Brederlow writes: > Hi, > > consider the code below that counts how often a value is printed. > > The reason why this code works is that the memory layout of a > variant with arguments is identical to a record with the same > types. The only difference is that a variant sets the tag of the memory > block to reflect which constructor it is (here Bar = 0, Baz = 1). > > But the code is fragile. It requires the use of Obj.magic and duplicates > the definitions of bar and baz (once as variant and once as record). If > the type of foo is changed but not the records then bad things will > happen. > > There are ways to do this without Obj.magic: > > type foo = Bar of bar_record | Baz of baz_record > type foo = Bar of int * int ref | Baz of float * int ref > > The first adds an indirection for every access to foo and breaks > matching. The second adds an indirection for every mutable value in the > type. In both cases the extra indirections increase the memory footprint > and runtime. > > > So why not allow mutable in variant types and some equivalence with > records? For example: > > type = of [mutable] [:label] [| ...] Please see [1], Alain Frisch has been working recently on implementing in-line records for constructor arguments. It's more implementation/design implications than people might think. [1] http://caml.inria.fr/mantis/view.php?id=5528 -- Wojciech