From: Jacques GARRIGUE <garrigue@kurims.kyoto-u.ac.jp>
To: 100060.2106@compuserve.com
Cc: caml-list@pauillac.inria.fr
Subject: Re: Oo.copy
Date: Tue, 28 May 1996 20:48:24 +0900 [thread overview]
Message-ID: <199605281148.UAA15218@orion> (raw)
In-Reply-To: <960525202556_100060.2106_JHB98-1@CompuServe.COM> (message from ESPERET PHILIPPE on 25 May 96 16:25:56 EDT)
>>>>> ESPERET PHILIPPE <100060.2106@compuserve.com> writes:
> I cannot understand how to access a fresh copy of an object with
> O'Caml (Oo seems normally to copy only pointers). Thank you if someone
> can explain me how to get such a copy (please see below for an example).
> One related problem is : the documentation for O'Caml exists but is not
> so thick ; it could be useful to have a list where true beginners in
> OCaml might ask their questions and find examples.
> class virtual 'a matrix(sz,init:int*'a) as self=
> val size=sz
> val m=Array.create_matrix sz sz init
> val zero=init
> virtual printa:'a -> unit
> (*method copy()=new matrix(size,zero)
> forbidden: One cannot create instances of the virtual class matrix*)
> method m=m
> method set(i,j,nval:int*int*'a)= m.(i).(j) <- nval
> method build(f:int->int->'a)=for i=0 to size-1 do
> for j=0 to size-1 do
> m.(i).(j)<- f i j done done;self
> method print()=for i=0 to size-1 do
> for j=0 to size-1 do
> self#printa m.(i).(j);
> print_char ';' done;
> print_newline() done
> end
> class int_matrix (sz:int)=
> inherit (int) matrix (sz,0)
> method printa=print_int
> end
> let m=new int_matrix 3;;
> (m#build (fun i j->i+j))#print();;
> m#print();;
0;1;2;
1;2;3;
2;3;4;
> let n=Oo.copy m;;
> (*let nn=m#copy();;*)
> m#set(1,1,66);;
> n#print();;
> (*nn#print();;*)
0;1;2;
1;66;3;
2;3;4;
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.map 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;;
This is difficult to have a general way, since mutable data structures
can get various forms.
Jacques
---------------------------------------------------------------------------
Jacques Garrigue Kyoto University garrigue@kurims.kyoto-u.ac.jp
<A HREF=http://wwwfun.kurims.kyoto-u.ac.jp/~garrigue/>JG</A>
next prev parent reply other threads:[~1996-05-28 12:04 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1996-05-25 20:25 Oo.copy ESPERET PHILIPPE
1996-05-28 11:48 ` Jacques GARRIGUE [this message]
1996-05-28 13:06 ` Oo.copy Didier Remy
1996-05-28 18:43 Oo.copy Andrew Conway
1996-05-29 1:53 ` Oo.copy Jacques GARRIGUE
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=199605281148.UAA15218@orion \
--to=garrigue@kurims.kyoto-u.ac.jp \
--cc=100060.2106@compuserve.com \
--cc=caml-list@pauillac.inria.fr \
/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