From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Delivered-To: caml-list@yquem.inria.fr Received: from nez-perce.inria.fr (nez-perce.inria.fr [192.93.2.78]) by yquem.inria.fr (Postfix) with ESMTP id E1495BB91 for ; Sat, 22 Jan 2005 07:50:13 +0100 (CET) Received: from pauillac.inria.fr (pauillac.inria.fr [128.93.11.35]) by nez-perce.inria.fr (8.13.0/8.13.0) with ESMTP id j0M6oDF1014078 for ; Sat, 22 Jan 2005 07:50:13 +0100 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 HAA08468 for ; Sat, 22 Jan 2005 07:50:12 +0100 (MET) Received: from kurims.kurims.kyoto-u.ac.jp (kurims.kurims.kyoto-u.ac.jp [130.54.16.1]) by concorde.inria.fr (8.13.0/8.13.0) with ESMTP id j0M6oAaJ002942 for ; Sat, 22 Jan 2005 07:50:12 +0100 Received: from localhost (suiren [130.54.16.25]) by kurims.kurims.kyoto-u.ac.jp (8.13.1/8.13.1) with ESMTP id j0M6o5ES000430; Sat, 22 Jan 2005 15:50:05 +0900 (JST) Date: Sat, 22 Jan 2005 15:49:09 +0900 (JST) Message-Id: <20050122.154909.132744958.garrigue@math.nagoya-u.ac.jp> To: colanderman@gmail.com Cc: caml-list@inria.fr Subject: Re: [Caml-list] Shallow copy of a record From: Jacques Garrigue In-Reply-To: <875c7e0705012120277c090ec8@mail.gmail.com> References: <875c7e0705012120277c090ec8@mail.gmail.com> X-Mailer: Mew version 4.1.53 on Emacs 21.3 / Mule 5.0 (SAKAKI) Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Miltered: at nez-perce with ID 41F1F7A5.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Miltered: at concorde with ID 41F1F7A2.000 by Joe's j-chkmail (http://j-chkmail.ensmp.fr)! X-Spam: no; 0.00; caml-list:01 foo:01 dup:01 lazy:01 dup:01 dynamically:01 define:01 jacques:01 jacques:01 unsafe:01 int:01 caml:02 modifying:02 construct:02 construct:02 X-Spam-Checker-Version: SpamAssassin 3.0.0 (2004-09-13) on yquem.inria.fr X-Spam-Status: No, score=0.0 required=5.0 tests=none autolearn=disabled version=3.0.0 X-Spam-Level: From: Chris King > Maybe I missed it in the docs, but is there a way to create a shallow > copy of a record without modifying any of its contents? I know > there's the {foo with a=b} construct, but this only works with at > least one assignment; I want to create an unmodified copy of the > record (akin to the {< >} construct for objects). This does not exist, and with good reason: there is no way in the type system to define a function which works on all records, but only records. So to do the copy you need to know at least one of the labels of the record, which in turn gives you its type. Another approach, which is potentially unsafe, is to use the Obj.dup function. You can make it a bit safer by ensuring (dynamically) that you only copy caml blocks or values: let copy (v : 'a) : 'a = let obj = Obj.repr v in if Obj.is_int obj then v else if Obj.is_block obj && let tag = Obj.tag obj in tag < Obj.lazy_tag || tag = Obj.double_array_tag then Obj.obj (Obj.dup obj) else invalid_arg "shallow copy" Jacques Garrigue