Description
This article is from the FAQ, by with numerous contributions by
others.
L15) How do I implement a copy (or clone) operation?
It is often useful to be able to make a genuine copy of an object. It is
currently being discussed to introduce a 'clone' operation into the object
pattern, which should take care of this copying automatically, but no
decision has been made as to how and when.
Until then, the following trick does the job:
P: (# (* internal P structures *)
copy:< (* generic copy operation *)
(# copyType: ##P;
theCopy: ^P;
do this(P)##->copyType##;
©Type[]->theCopy[];
(* insert here code to implement the copying
* of internal P structures into theCopy *)
INNER copy;
(* possible finalization of the copying process *)
exit theCopy[]
#)
#);
Q: P(# (* internal Q structures *)
copy::<
(# Qcopy: ^Q
do theCopy[]->Qcopy[];
(* insert here code to implement the copying
* of internal Q structures into Qcopy *)
INNER copy;
(* possible finalization of the Q copying process *)
#)
#);
R: Q(# (* internal R structures *)
copy::<
(# Rcopy: ^R
do theCopy[]->Rcopy[];
(* insert here code to implement the copying
* of internal R structures into Rcopy *)
INNER copy;
(* possible finalization of the R copying process *)
#)
#);
Given the declarations
a: @R;
aCopy: ^R;
then
a.copy->aCopy[]
will make a copy of the a object with the proper qualification (here R), and
a state that is a copy of the state of a.
Note: The
Rcopy: ^R
do theCopy[]->Rcopy[]
part of the copy further binding is an inconvenience, but is necessary to
persuade the type system to allow remote access to the R-parts of the
theCopy object.
 
Continue to:
Share and Enjoy
Bookmark this story so others can enjoy it:
Tags
programming