Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Deep copy
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Nicolas Cannasse <warplayer@f...>
Subject: Re: [Caml-list] Deep copy
> > I'm writting a program that manipulates a graph structure and I need to
> > make deep copies of some of the graph nodes. The function Oo.copy
donesn't
> > perform a deep copy and I haven't found any other that could make it.
>
> You mean something like creating a new object on the heap and copying over
the
> contents of an existing object.
>
> If this were C++ all you had to do would be invoking the copy constructor
>  Node* x = new Node(old_node);

Errr....
You're supposing here that either the copy constructor is implemented by the
class writer or then that you're using the default copy constructor which
only copy fields, and then do not perform any deep-recursion... It's even
worst because pointers are duplicated so you can get very-unsafe behavior...

> From the documentation OO.copy does look like a deep copy operation:
>
> val copy : < .. > -> < .. >
> Oo.copy o returns a copy of object o, that is a fresh object with the same
> methods and instance variables as o

The "copy" notion is ambiguous, especially when you have mutable fields.
Does the modification of such a field on a copy of an object do influence
the original object ? Or is copy only the image of an object at a given time
? And what if the object manipulate opened streams ?
Not so easy to resolve... I would recommand then to write a copy operator
"by hand".

Nicolas Cannasse

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners