[squeak-dev] Re: Dictionary>>addAll:
Andreas Raab
andreas.raab at gmx.de
Tue Aug 24 15:08:34 UTC 2010
On 8/24/2010 2:50 AM, Levente Uzonyi wrote:
> On Mon, 23 Aug 2010, Andreas Raab wrote:
>
>> Hm ... tell me what you *think* this code would do:
>>
>> dict := Dictionary withAll: {'foo' -> 'bar'. 'hello' -> 'world'}.
>>
>> then run it. Does that seem right to you?
>
> Yes and no. :) Dictionary >> #addAll: expects a "KeyedCollection" as
> argument. Array is a "KeyedCollection" with integer keys.
> But one could expect that #addAll: works like #add: which is not the case.
> We could change #addAll: to use #associationsDo: instead of
> #keysAndValueesDo: as Nicolas said, but it might break some code.
I cannot really imagine which code that would break. Assuming that
you've got a dictionary would you really use, i.e., aDictionary addAll:
#('hello' 'world') and expect the result to be 1 -> 'hello' and 2 ->
'world'? This was *totally* unexpected for me and at first I couldn't
even understand what was going wrong until I realized that the usage of
#withAll: caused it.
So yes, I think we should fix that to ensure that #addAll: is consistent
with #add: as it is in all other collection types.
> If you just want to build a Dictionary from an Array of Associations,
> then #newFrom: does what you need (it uses the #associationsDo: trick).
Yes, both #newFrom: and #as: appear to do the trick - the above is more
about consistency and least surprises.
Cheers,
- Andreas
More information about the Squeak-dev
mailing list
|