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: [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: John Prevost <j.prevost@c...>
Subject: Re: [Caml-list] Deep copy
>>>>> "ab" == Alessandro Baretta <> writes:

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

The code he gave is correct.  The problem arises not when the object
itself contains a mutable field, but when one of its fields contains a
mutable value.  (An object with a mutable field, a string, an array, a
record with a mutable field, a ref, etc.)

# class a = object
    val s = "     "
    method get x = s.[x]
    method set x c = s.[x] <- c
class a :
    method get : int -> char
    method set : int -> char -> unit
    val s : string
# let x = new a;;
val x : a = <obj>
# x #get 0;;
- : char = ' '
# x #set 0 'a';;
- : unit = ()
# x #get 0;;
- : char = 'a'
# let y = Oo.copy x;;
val y : a = <obj>
# y #get 0;;
- : char = 'a'
# y #set 0 'b';;
- : unit = ()
# y #get 0;;
- : char = 'b'
# x #get 0;;
- : char = 'b'

As far as I can tell, the poster to whom you are responding was simply
pointing this out: if a field contains a mutable value (whether the
field itself is mutable or not), the contents of that field are
copied, which results in physical equality of the field values in
those cases where physical identity is important.  He doesn't appear
to have claimed that the copied fields themselves have physical


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: