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?
Mark
Mark Greenaway mark@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.
"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@riskmetrics.com
At 12:30 AM -0800 12/7/00, Andrew P. Black wrote:
SequenceableCollection >> asDictionary... 6/12/96 sw 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.
Andrew:
I have no explicit recollection of this weirdo method or what purpose it served at the time. It originated back in the very early days of Squeak, before it was called Squeak, before it was public, before SC had left Apple.
Why is it part of the image? Just one of many things that happened to be in the image at the time Squeak first went out. We always had various end-user-oriented experiments going concurrently with the ongoing work on the mainstream professional programmers' system, and one of them in mid 1996 was something called "HyperSqueak"; quite possibly this errant method was one that provided support for some aspect of that experiment. The HyperSqueak classes have long since been stripped from the system, but some of the underlying support methods written for that experiment still lurk in generic classes, and this is probably the explanation for this one.
Already by Squeak 1.0, we were well past the point where anyone could reasonably make a complete, manual pass through all the methods in the system before a release. Thus, various dubious things slipped through the cracks, and these turn up from time to time and eventually get dealt with.
In an update recently posted to the internal update stream, I've expunged this particular dubious method.
Thanks,
-- Scott
PS: Sorry for the delay in responding to this.
At 20:41 -0800 01.01.05, Scott Wallace wrote:
Andrew:
I have no explicit recollection of this weirdo method or what purpose it served at the time. It originated back in the very early days of Squeak, before it was called Squeak, before it was public, before SC had left Apple.
Well, thanks for the explanation Scott. Certainly there is no need to apologize for the delay in responding -- this was hardly a high priority! I'm just a bit of a computing archeologist at heart, and get curious about these things.
Andrew
squeak-dev@lists.squeakfoundation.org