[Newbies] Re: ordinary #hash and Dictionary [was: Recap: How to
empty a collection]
Klaus D. Witzel
klaus.witzel at cobss.com
Thu Feb 21 06:30:41 UTC 2008
On Wed, 20 Feb 2008 22:12:57 +0100, nicolas cellier wrote:
> Klaus D. Witzel a écrit :
>> On Wed, 20 Feb 2008 00:59:57 +0100, nicolas cellier wrote:
>> ...
>>> I see, become: does exchange #identityHash, and that makes our
>>> IdentityDictionary work, god thanks, but there is no such provision
>>> for ordinary #hash and Dictionary...
>> But there are sufficient provisions in place, since Smalltalk-80, and
>> most Dictionary users know about them :)
>>
>>> However, there are plenty of ordinary things that would have the same
>>> result:
>>>
>>> | key1 key2 dic |
>>> key1 := 'abc' copy.
>>> key2 := 'abd' copy.
>>> dic := Dictionary new.
>>> dic at: key1 put: 1.
>>> dic at: key2 put: 2.
>>> key1 at: 1 put: $z.
>> dic rehash "synopsis: re-establish hash invariants, if any ".
>>
>>> {dic includesKey: key1.
>>> dic keys includes: key1.}
>>>
>>> So i propose newbies do not use #at:put: considering the danger about
>>> Dictionary not finding their keys...
>> There's no danger with Dictionary not finding its keys, unless you
>> yourself do not follow the protocol.
>> Smalltalk has no such problems; developers use #rehash after they
>> changed the #= of keys in Set and subclasses; and please, don't tell
>> the newcomers the contrary :)
>>
>
> Agree.
> Thanks for this good lesson based on my so dumb example.
> Wouldn't this deserve a longer (Dictionary comment) ?
You're right, the invariant is *essential* for Set and its subclasses,
alas no word about it in its class comment. Please open a bug report (with
severity "text"); current policy is that this then gets included in the
next release.
>>> That's too much.
>> No not too much; in other languages (especially the "popular" ones)
>> you are not even allowed to change you strings-now *that* is too much
>> ;-)
>> But in Smalltalk you are supported, right from the beginning; happy
>> Smalltalking everybody :)
>> /Klaus
>
> Too much was the idea of forbidding #become: or #at:put: for a wrong
> reason (Dictionary rehash).
>
> Nicolas
More information about the Beginners
mailing list