From: Alessandro Baretta <alex@baretta.com>
To: zze-MARCHEGAY Michael stagiaire FTRD/DTL/LAN
<michael.marchegay@rd.francetelecom.com>,
Ocaml <caml-list@inria.fr>
Subject: Re: [Caml-list] Deep copy
Date: Mon, 15 Jul 2002 21:53:53 +0200 [thread overview]
Message-ID: <3D332851.1080909@baretta.com> (raw)
In-Reply-To: <0489A7888F080B4BA73B53F7E145F29A1B0AF5@LANMHS20.rd.francetelecom.fr>
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
next prev parent reply other threads:[~2002-07-15 19:46 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2002-07-15 15:24 zze-MARCHEGAY Michael stagiaire FTRD/DTL/LAN
2002-07-15 15:24 ` Eray Ozkural
2002-07-15 18:18 ` Alessandro Baretta
2002-07-15 19:53 ` Alessandro Baretta [this message]
2002-07-15 22:35 ` John Prevost
2002-07-15 23:03 ` Alessandro Baretta
2002-07-16 8:19 ` John Prevost
-- strict thread matches above, loose matches on Subject: below --
2002-07-15 10:06 zze-MARCHEGAY Michael stagiaire FTRD/DTL/LAN
2002-07-15 13:42 ` Ken Wakita
2002-07-15 14:43 ` Eray Ozkural
2002-07-15 15:33 ` Ken Wakita
2002-07-15 23:09 ` Ken Wakita
2002-07-15 15:02 ` Eray Ozkural
2002-07-15 15:27 ` Nicolas Cannasse
2002-07-15 15:40 ` Eray Ozkural
2002-07-15 16:22 ` sebastien FURIC
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=3D332851.1080909@baretta.com \
--to=alex@baretta.com \
--cc=caml-list@inria.fr \
--cc=michael.marchegay@rd.francetelecom.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox