[FIX] Re: Dictionary initialization syntax

Doug Way dway at riskmetrics.com
Mon Dec 11 03:35:11 UTC 2000


"Andrew P. Black" wrote:
> 
> Normally, I expect that
> 
>         {1->#a. 2->#b. 3->#c} as: Dictionary
> and     {1->#a. 2->#b. 3->#c} asDictionary
> 
> would do the same thing, in fact, it is usual for the asFoo method to
> be implemented using self as: Foo, which in turn is implemented using
> Foo newFrom: self.  However, as Mark correctly points out,
> asDictionary is weird.  Moreover, there are no send of this message
> in my image.
> 
> SequenceableCollection >> asDictionary
>         "Answer a Dictionary whose keys are string versions of my
> indices and whose values are my elements.  6/12/96 sw"
> 
>         | aDictionary |
>         aDictionary _ Dictionary new.
>         1 to: self size do:
>                 [:i | aDictionary add:
>                         (Association key: i printString value: (self at: i))].
>         ^ aDictionary
> 
> I wonder if anyone (sw?) can shed light on where this method came
> from, and why it is part of the image?   I think that Mark had the
> right idea when he suggested changing it, or at least renaming it to
> something like asDictionaryKeyedByStrings.

I agree that it seems too specialized to be called "asDictionary",
asDictionaryKeyedByStrings might be better.  Also, it's probably better
for asXXX to be implemented like "as: XXX" as a default, as you say.

Perhaps one reason that it is the implementation of asDictionary is that
it does work for pretty much any SequenceableCollection, whereas "...
as: Dictionary" will break unless you happen to have a collection of Associations.

I was just going to post an [ENH] changing asDictionary to implement
"as: Dictionary", which is always a good way to force the issue, but
given the problem above, maybe I will hold off.

- Doug Way
  dway at riskmetrics.com





More information about the Squeak-dev mailing list