Dictionary keys that exist but can't be found
tim at jump.net
Thu Apr 11 14:07:45 UTC 2002
>I've had this problem with the Dictionary class before using a class that I
>created as the key. Are there any caveats with this class I may not be aware
Here's one that I got bit by -- don't use Associations as keys:
d := Dictionary new.
d at: (1 -> 2) put: 3.
d at: (1 -> 7). "answers 3".
This is because Association inherits "=" from LookupKey, which only
compares keys for equality, so two Associations with the same key but
different values still compare equal.
I'm not sure if this is an oversight, or if Associations are purposely
this way for some subtle reason in implementing the Dictionary class.
I just tried an experiment, implementing "hash" and "=" for Associations:
^ key hash bitXor: value hash
self species = anAssociation species
ifTrue: [^key = anAssociation key and: [value = anAssociation value]]
Nothing immediately fell over, and my broken test case now works.
Does anyone know of anything in the image that relies upon associations
comparing equal even if their respective values are different?
More information about the Squeak-dev