From mboxrd@z Thu Jan 1 00:00:00 1970 Received: (from weis@localhost) by pauillac.inria.fr (8.6.10/8.6.6) id MAA21988 for caml-redistribution; Wed, 29 May 1996 12:00:03 +0200 Received: (from remy@localhost) by pauillac.inria.fr (8.6.10/8.6.6) id PAA08787; Tue, 28 May 1996 15:06:43 +0200 Message-Id: <199605281306.PAA08787@pauillac.inria.fr> Subject: Re: Oo.copy To: garrigue@kurims.kyoto-u.ac.jp (Jacques GARRIGUE) Date: Tue, 28 May 1996 15:06:42 +0200 (MET DST) Cc: 100060.2106@compuserve.com, caml-list@pauillac.inria.fr In-Reply-To: <199605281148.UAA15218@orion> from "Jacques GARRIGUE" at May 28, 96 08:48:24 pm From: Didier.Remy@inria.fr (Didier Remy) Reply-to: Didier.Remy@inria.fr Organization: INRIA, BP 105, F-78153 Le Chesnay Cedex Phone: (33) 1 3963 5317 -- Sec: (33) 1 3963 5684 -- Fax: (33) 1 3963 5330 Web: http://pauillac.inria.fr/~remy MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: weis > The problem is that your array is shared between the two copies. > There are various specific ways to solve it. > One is to add an "unsharing" method in the virtual class, which is > applied after copying. > > val mutable m = ... > method unshare = m <- Array.map Array.copy m > method copy = let o = Oo.copy m in o#unshare; o > > Then the problem is solved by using > > let n = m#copy;; A shorter way is val m = ... method copy = {< m = Array.map Array.copy m >} This is also safer since the state m does not need to be mutable. > This is difficult to have a general way, since mutable data structures > can get various forms. As you have understood, the function Oo.copy implements a superficial copy. It would be possible to provide a (system) deep copy that would duplicate the whole graph structure in the memory, using a mechanism similar the implementation of export and import but not failing on functions. This copy could also work for any ML value, not just objects. However, it is not clear that this is often what the user wants, and we did not provide it. Superficial and deep copy can be polymorphic other all objects and therefore provided in a (system) library. Any intermediate copy is arbitrary and therefore should be programmed by the user, usually as a method of the corresponding objects. Didier.