help-smalltalk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-smalltalk] CharacterArray class>>#lineDelimiter and Dictionary


From: Paolo Bonzini
Subject: Re: [Help-smalltalk] CharacterArray class>>#lineDelimiter and Dictionary>>#addAll: both fail
Date: Fri, 23 Mar 2007 15:26:38 +0100
User-agent: Thunderbird 1.5.0.10 (Macintosh/20070221)

> Taken literally, the standard says "nothing but Dictionary".  But my
> point is, if a Dictionary is a Collection of Associations, any such
> Collection can be used.

The point is that it is not.  The standard has no mention of
Associations at all, and that's a really good thing.

While the standard does not give any relationship between
Dictionaries and any other collection, there is only one that is
coherent with the rest of the standard:  keyed collections in
Smalltalk are SequenceableCollections and Dictionaries (the keys
being integers in the case of seq. collections).  Keyed collections
would be those that implement #keysAndValuesDo: (and if you search
for "keyed" in the standard, you'll see what I mean).

> This isn't a violation of standard.  It's
> interpretation.  That's what "abstract" in "<abstractDictionary>" means.
>  "If it walks like a duck and quacks like a duck, ..."

... but then, since, #('a' 'b' 'c') implements #keysAndValuesDo:,
"Dictionary withAll: #('a' 'b' 'c')" will give a
Dictionary with 1->'a', 2->'b', and 3->'c'.  This also can be
seen as "If it walks like a duck and quacks like a duck".

But the point is that the standard guys were *right* in dropping
Associations and "#add:" from it.  "#add:" makes no sense with
external keys, and it percolates Associations (which are an
implementation detail) to the definition of Dictionary.

One possibility could be to extend the {...} syntax to something
like "OrderedCollection {...}", "Dictionary {...}", and have it send
messages like "#braceWithAll:".  Then you would have:

     Array >> braceWithAll: x
         ^self

     Collection >> braceWithAll: x
         ^self withAll: x

     Dictionary >> braceWithAll: x
         | dict |
         dict := self new: x size * 2.
         "in the loop we want 'self add: x' maybe?"
         x do: [ :each | dict at: x key put: x value ].
         ^dict

The compiler might know of a few classes and optimize them;
or maybe not.

Paolo




reply via email to

[Prev in Thread] Current Thread [Next in Thread]