Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
Re: Oo.copy
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 1996-05-29 (10:00)
From: Didier.Remy@i...
Subject: Re: Oo.copy

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