3 May 2011 16:05
Re: Using #deepCopy, I have trouble understanding why it the code fails.
Alan Knight <knight <at> acm.org>
2011-05-03 14:05:59 GMT
2011-05-03 14:05:59 GMT
I note that there is no #deepCopy method in the base system. There used to be, a very long time ago, but it was removed because of the ambiguity of what it was actually expected to do in different cases. So if you're using it, it's presumably been added by you or by some package you're using.
May 3, 2011 5:31 AMThe answer is simple.
Simple #copy prefers to do #shallowCopy, i.e. make a copy where all inst.vars are identical to the
original. A #deepCopy creates new objects on every level in the depth, until all nodes in the
original object graph have copied counterparts.
When you make a comparison, the default test is for identity. If you have your own implementation of
#=, then you will check equality of various inst.vars. But it is very likely that your object graph
contains instances from "system" classes, i.e. classes that are not from your domain. These classes
have their own understanding of being equal, in particular those that simply inherit #= from Object
with "^self == anObject", then the equality test *must* fail at a certain level.
This means that "x = x deepCopy" should return false *in general*, unless the object graph of x
contains only instances from your domain or from "simple" data types, such as Number, String, Date, etc.
Smalltalk is such a wonderful environment where you can learn how the equality test is done by
simple use of the Debugger. Just run "x = x deepCopy" with your favorite object x, and use "Debug
it" instead of "Do it". Follow the flow by stepping into each comparison and wait for your "aah" effect.
vwnc mailing list
vwnc <at> cs.uiuc.edu
May 3, 2011 4:56 AMHello.
The code eventually executes #== (inherited from Object) to
compare two MyContact instances, because MyContact does not
define its own version of #=. You can of course implement
it to something like this:
^self class == anObject class
and: [name = anObject name
and: [emailAddress = anObject emailAddress]].
And, as noted in Object>>=, you should define your own
version of #hash as well if you do so:
^(self class identityHash bitXor: name hash) bitXor: emailAddress hash.
All objects for which #= evaluates to true must have the
same #hash value. The #hash is used for quick lookup in
Set and Dictionary collections.
_______________________________________________ vwnc mailing list vwnc <at> cs.uiuc.edu http://lists.cs.uiuc.edu/mailman/listinfo/vwnc