Dictionary #addAll: problem

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Dictionary #addAll: problem

Christoph J. Bachinger
Adding a Dictionary contents to another Dictionary via
addAll: causes an error due to the lack of an addAll:
in Dictionary.

An Example could be this

addAll: newElements
        newElements associationsDo: [:each | self add: each].
        ^newElements


or maybe better to be compatible with the older behavior

addAll: newElements
     newElements isDictionary   "Dolphin knows no isDictionary"
         ifTrue: [ newElements associationsDo: [ :assoc | self add: assoc] ]
         ifFalse: [ newElements do: [ :each | self add: each ] ].
     ^newElements

or do I understand anything wrong.


Reply | Threaded
Open this post in threaded view
|

Re: Dictionary #addAll: problem

Peter van Rooijen
"Christoph J. Bachinger" <[hidden email]> wrote in message
news:bo8das$1a270u$[hidden email]...

> Adding a Dictionary contents to another Dictionary via
> addAll: causes an error due to the lack of an addAll:
> in Dictionary.
>
> An Example could be this
>
> addAll: newElements
> newElements associationsDo: [:each | self add: each].
> ^newElements
>
>
> or maybe better to be compatible with the older behavior
>
> addAll: newElements
>      newElements isDictionary   "Dolphin knows no isDictionary"
>          ifTrue: [ newElements associationsDo: [ :assoc | self add:
assoc] ]
>          ifFalse: [ newElements do: [ :each | self add: each ] ].
>      ^newElements
>
> or do I understand anything wrong.

Christoph,

I'm not entirely sure I understand your issue completely, but perhaps these
observations are useful  to you:

A <dictionary>, when seen as a <collection>, is conventionally considered to
be the collection of its values (i.e., not of its associations - they are an
implementation detail: many dictionaries are not implemented using
associations at all).

Using a <dictionary> as the receiver or as the argument of #addAll: is such
a case (of seeing a <dictionary> as a <collection>).

The other relevant convention for adding things to dictionaries is that if
you add them using #add: (as opposed to #at:put:) is that the things respond
to #key, so that #add: can be implemented as:

Dictionary>>add: anObject

self at: anObject key put: anObject

To make things even more complex, several Smalltalk dialects implement
#value in Object to return self, so that they can allow associations to be
added to a Dictionary by implementing:

Dictionary>>add: anObject

self at: anObject key put: anObject value

Finally, there is the general observation that #addAll: modifies the
receiver <collection> in the same way as multiple #add:'s in a #do: loop
over the argument <collection>.

Hope this helps instead of adding to the confusion ;-).

Cheers,

Peter van Rooijen
Amsterdam