Why set identity of #becomeForward: argument to the receiver?
David Jones
squeak at night.dircon.co.uk
Thu Jul 29 18:15:00 UTC 1999
Given two objects, A and B, if I evaluate:
A becomeForward: B
Then
*Both A and B point to the original contents of B
but
*Both A and B now have the original #identityHash value of A
The identity of the existing pointers to B has been modified.
A := 'A'.
B := 'B'.
A identityHash. "=1551"
B identityHash. "=298"
A becomeForward: B.
A. "= 'B' "
B. "= 'B' "
A identityHash. "=1551"
B identityHash. "=1551"
The upshot of this is if I had created an IdentityDictionary before the
#becomeForward, such as
(C := IdentityDictionary new)
at: A put: 1;
at: B put: 2.
Then after the #becomeForward: I find
(C at: A) = 1
(C at: B) = 1
I would have imagined that after the #becomeForward: that A would now take
the original #identityHash value of B. So
(C at: A) = 2
(C at: B) = 2
I guess its a case of horses for courses, if either are in identity hashed
collections you have to rehash, but I would have imagined that if you try to
become something else, then that something else wouldn't be modified in the
process.
Perhaps at least the comment for Object #becomeForward: should warn about
the #identityHash: value changing for the argument, and the requirement to
#rehash effected identity hashed collections.
David Jones
http://www.night.dircon.co.uk/
|a b c aPastIdentityHash bPastIdentityHash |
Transcript show: '--------------------------------------------';cr.
a := 'a' copy. aPastIdentityHash := a identityHash.
b := 'b' copy. bPastIdentityHash := b identityHash.
(c := IdentityDictionary new) at: a put: 1; at: b put: 2.
Transcript show: 'A:', a,aPastIdentityHash printString,'
B:',b,bPastIdentityHash printString;cr.
Transcript show: '(c at: A)=',(c at: a) printString,' (c at: B)=',(c at: b)
printString;cr.
a become: b."**** become action"
Transcript show: '=== a become: b ===';cr.
Transcript show: 'A:', a,a identityHash printString,' B:',b,b identityHash
printString;cr.
Transcript show: '(c at: A)=',(c at: a) printString,' (c at: B)=',(c at: b)
printString;cr.
Transcript show: '--------------------------------------------';cr.
a := 'a' copy. aPastIdentityHash := a identityHash.
b := 'b' copy. bPastIdentityHash := b identityHash.
(c := IdentityDictionary new) at: a put: 1; at: b put: 2.
Transcript show: 'A:', a,aPastIdentityHash printString,'
B:',b,bPastIdentityHash printString;cr.
Transcript show: '(c at: A)=',(c at: a) printString,' (c at: B)=',(c at: b)
printString;cr.
a becomeForward: b."**** become action"
Transcript show: '=== a becomeForward: b ===';cr.
Transcript show: 'A:', a,a identityHash printString,' B:',b,b identityHash
printString;cr.
Transcript show: '(c at: A)=',(c at: a) printString,' (c at: B)=',(c at: b)
printString;cr.
" An example of the output from the above for #become & #becomeForward
--------------------------------------------
A:a2718 B:b4063
(c at: A)=1 (c at: B)=2
=== a become: b ===
A:b2718 B:a4063
(c at: A)=1 (c at: B)=2
--------------------------------------------
A:a3118 B:b1583
(c at: A)=1 (c at: B)=2
=== a becomeForward: b ===
A:b3118 B:b3118
(c at: A)=1 (c at: B)=1
"
More information about the Squeak-dev
mailing list
|