veryDeepCopy

Tim Olson tim at jump.net
Thu Oct 4 17:33:07 UTC 2001


>>veryDeepCopy
>>   (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
>
>could you say some more about veryDeepCopy? when i might want to use it
>instead of just deepCopy?


You would use veryDeepCopy when you have an object that can have multiple 
instances of another object, and you want to create a copy where those 
multiple instances point to the same copy (but not to the original) -- 
i.e. retain the identity mapping.

I think its main use is in Morphic "duplicate" (green halo button).

Here's a concrete example of the differences.

"a common association"
assoc := 'key' -> 'value'.

"original container"
original := OrderedCollection with: assoc with: assoc.

"copy makes a copy of the top-level object, and fills it
 with the original's instance variables and indexed values"
copy := original copy.
copy == original.  							"false"
copy first == original first.  					"true"
copy first == copy second. 					"true"

"deep copy copies the original structure recursively,
 making a copy of every mutable object"
copy  := original deepCopy.
copy first == original first. 					"false"
copy first == copy second. 					"false"

"veryDeepCopy 
copy := original veryDeepCopy.
copy first == original first. 					"false"
copy first == copy second. 					"true"


[Note: I originally started with a Point as the shared object in the 
example above, but quickly found that for some reason, Points are assumed 
to be immutable in Morphic, so they don't participate in veryDeepCopy 
like I'd expect].



     -- tim






More information about the Squeak-dev mailing list