Dictionary keys that exist but can't be found
Ted Kaehler
Ted-sqk at legenda.com
Thu Apr 11 16:16:59 UTC 2002
Tim,
IdentityDictionary is what you want to use. It will allow
Associations to be keys in just the way you want.
The changes you are suggesting to Association #= and #hash
are ill-advised.
--Ted.
At 9:07 AM -0500 4/11/02, Tim Olson wrote:
> >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
>>of?
>
>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:
>----
>hash
>
> ^ key hash bitXor: value hash
>
>
>= anAssociation
>
> self species = anAssociation species
> ifTrue: [^key = anAssociation key and: [value =
>anAssociation value]]
> ifFalse: [^false]
>----
>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?
> -- tim
--
Ted Kaehler http://www.squeakland.org/~ted/
(home) 3261 Montecito Drive, Las Vegas, NV 89120. voice (702) 456-7930
If builders built buildings the way programmers write programs,
then the first woodpecker to come along would destroy civilization.
--Weinberg's Second Law
More information about the Squeak-dev
mailing list
|