[FIX] Re: Dictionary initialization syntax
Andrew P. Black
apb at cse.ogi.edu
Thu Dec 7 08:30:04 UTC 2000
Mark Greenaway <mark at knobbits.org> writes:
>I'm fairly new to Smalltalk and have been learning mostly by looking at
>web pages and bits of Squeak code. I was wondering if there was a way to
>initialise dictionaries in the same way you can in Python i.e.
>
>dict = {1 : 2, 3 : 4}
>
>I was hoping something like {1->2. 3->4} asDictionary might work, but
>asDictionary converts the keys to strings using printString. Should I just
>change asDictionary or is there a better way?
Well, Dictionary Class >> newFrom: aDictOrArray contains a couple of
examples of ways to do, this, namely
Dictionary newFrom: {1->#a. 2->#b. 3->#c}
{1->#a. 2->#b. 3->#c} as: Dictionary
But the comments and argument name in that method were such that you
were unlikely to find it. I've changed the comments, and attach the
fixed version.
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.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Dictionary_class-newFrom.st
Type: application/octet-stream
Size: 731 bytes
Desc: not available
Url : http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20001207/b48fb5a8/Dictionary_class-newFrom.obj
More information about the Squeak-dev
mailing list
|