[squeak-dev] Re: Dictionary>>addAll:

Levente Uzonyi leves at elte.hu
Tue Aug 24 23:19:29 UTC 2010



On Tue, 24 Aug 2010, Andreas Raab wrote:

> 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.

This method is from 2003, so there may be code out there which expects 
this behavior, even unintentionally.

>
> So yes, I think we should fix that to ensure that #addAll: is consistent with 
> #add: as it is in all other collection types.

Ok.


Levente

>
>> 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