copy, deepCopy, et al
Last updated at 4:06 pm UTC on 24 April 2017
Method clone vs shallowCopy
From: Tim Olson <email@example.com>
Date: Thu, 4 Oct 2001
Here's something I posted to comp.lang.lisp about Squeak's copying methods when this discussion came up there:
The Object class provides a number of default copy operations:
make a copy of the object which shares instance variables (shallow)
"public interface" to clone, used to override for things like singletons
defaults to shallowCopy, general copying method overriden by subclasses as required
copy of object with shallow copy of instance variables
(recursive) copy of object with deepCopy of all instance variables
(probably should really be named "structureCopy" or something) copies the object, using a dictionary to keep track of the mapping from old objects to new objects, so that various identities are preserved.
It is up to the various subclasses to override any of these as required, or implement a new copying interface.
If you want to copy some instance variables deeply and others not, see the discussion in the class comment of DeepCopier for further guidance. – Ross Boylan; RossBoylan@stanfordalumni.org