Version française
Home     About     Download     Resources     Contact us    
Browse thread
RE: [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: Alessandro Baretta <alex@b...>
Subject: Re: [Caml-list] Deep copy
zze-MARCHEGAY Michael stagiaire FTRD/DTL/LAN wrote:
> In fact, if an object 'a' contains an attribute 'b' that is another 
> object, Oo.copy on 'a' gives a copy of 'a' in which the attribute
> 'b' is **physically** equal to the copied attribute from 'a'.

I agree so far. But did you actually test the code below? I 
have reason to believe you are mistaken in believing that 
mutable fields are shared between Oo.copied objects, in such 
a way that assignment to such a field in one object will 
result in a modification in the value of the same field in 
all copies. I would consider such a behavior a major design 
flaw in the language.

The following toplevel session proves my point:

# class a i = object val mutable c = i method m = c <- c + 1 
; print_string ((string_of_int c) ^ " ") end;;
class a : int -> object method m : unit val mutable c : int end
# let a1 = new a 0;;
val a1 : a = <obj>
# let a2 = Oo.copy a1;;
val a2 : a = <obj>
# for i = 1 to 10 do a1 # m done;;
1 2 3 4 5 6 7 8 9 10 - : unit = ()
# for i = 1 to 15 do a2 # m done;;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - : unit = ()
# for i = 11 to 15 do a1 # m done;;
11 12 13 14 15 - : unit = ()

Alex

> Example:
> class a = 
>   object 
>     val mutable b = new b
>     method get_b = b
>   end
> and b = 
>   object
>     val mutable c = "1"
>     method set_c x =  c <- x
>     method print_c = print_string ("c=" ^ c ^ "\n")  
>   end
> 
> let aa = new a
> let aa' = Oo.copy aa
> 
> let _ = 
>   aa#get_b#set_c "3";
>   aa#get_b#print_c;
>   aa'#get_b#print_c
> 
> gives:
> c=3
> c=3
> 
> whereas a deep copy would give:
> c=3
> c=1

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