Re: Oo.copy

Didier Remy (Didier.Remy@inria.fr)
Tue, 28 May 1996 15:06:42 +0200 (MET DST)

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)
In-Reply-To: <199605281148.UAA15218@orion> from "Jacques GARRIGUE" at May 28, 96 08:48:24 pm
From: Didier.Remy@inria.fr (Didier Remy)

> 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.