From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from majordomo@localhost) by pauillac.inria.fr (8.7.6/8.7.3) id FAA08699; Thu, 28 Jun 2001 05:42:52 +0200 (MET DST) X-Authentication-Warning: pauillac.inria.fr: majordomo set sender to owner-caml-list@pauillac.inria.fr using -f Received: from concorde.inria.fr (concorde.inria.fr [192.93.2.39]) by pauillac.inria.fr (8.7.6/8.7.3) with ESMTP id FAA08717 for ; Thu, 28 Jun 2001 05:42:51 +0200 (MET DST) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.11.1/8.10.0) with ESMTP id f5S3gmr07654 for ; Thu, 28 Jun 2001 05:42:49 +0200 (MET DST) Received: from localhost (suiren.kurims.kyoto-u.ac.jp [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.9.3/3.7W) with ESMTP id MAA19652; Thu, 28 Jun 2001 12:42:43 +0900 (JST) To: checker@d6.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] anonymous record copy? In-Reply-To: <200106280248.TAA15050@smtp2-cm.mail.eni.net> References: <200106280248.TAA15050@smtp2-cm.mail.eni.net> X-Mailer: Mew version 1.94.2 on Emacs 20.7 / Mule 4.0 (HANANOEN) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit Message-Id: <20010628124243M.garrigue@kurims.kyoto-u.ac.jp> Date: Thu, 28 Jun 2001 12:42:43 +0900 From: Jacques Garrigue X-Dispatcher: imput version 20000228(IM140) Sender: owner-caml-list@pauillac.inria.fr Precedence: bulk > How do you copy a record without a) updating anything in the record, > and b) not knowing the type of the record? > > type foo = { mutable bar : int } > > let a = { bar = 12 } > > let copy_record = ??? > > (* hopefully copy_record is 'a -> 'a, not foo -> foo *) > > let b = copy_record a > > b.bar <- 234 (* should not modify a.bar *) The problem is that if the type were 'a -> 'a, it would not apply only on records, but on just anything. Constrast this with Obj.copy, which applies on any object, but only on objects. I believe the natural way to write the copy of a record is {x with bar = x.bar}. You need to have at least one field, in order to know the actual type of the record, which gives you the list of fields to copy. Another way would be to use the Obj module, which is implementation dependent. The block must be marked either by a constructor tag or double_array_tag, otherwise you can break everything. In particulars objects have an oid, and should not be copied this way. let copy_record (r : 'a) : 'a = let obj = Obj.repr r in if Obj.is_int obj || Obj.size obj = 0 then r else let tag = Obj.tag obj in if tag < Obj.object_tag || tag = Obj.double_array_tag then Obj.obj (Obj.dup obj) else invalid_arg "copy_record" Cheers, Jacques Garrigue ------------------- Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr