What is the difference between a collection of associations and a dictionary in Smalltalk?
Asked Answered
A

2

5
| dict |
dict := #{'foo'->'brown'. 'bar'->'yellow'.
          'qix'->'white'. 'baz'->'red'. 'flub'->'green'} asDictionary.
dict at: 'qix'

If I PrintIt, I get 'white'. If I remove 'asDictionary', I still get 'white'. What does a dictionary give me that a collection of associations doesn't?

Ardeliaardelis answered 5/6, 2015 at 13:46 Comment(0)
K
9

Expression like #{exp1 . sxp2 . exp3} is specific and creates a HashedCollection, which is a special kind of dictionary where keys are strings (probably in Javascript you use things like this a lot).

In other smalltalks there is no expression like that. Instead array expressions which look like: {exp1 . sxp2 . exp3} (there is no leading #) were introduced in and are also available in (which is a fork of Squeak) and Amber. Now the array expression creates an Array and so you have to use integers for #at: message. For example dict at: 2 will return you an association 'bar'->'yellow' because it is on the second position of the array you've created.

#asDictionary is a method of a collection that converts it into a dictionary given that the elements of the collection are associations. So if you want to create a dictionary with keys other than strings, you can do it like this:

dict := {
    'foo' -> 'brown'  .
        1 -> 'yellow' .
    3 @ 4 -> 'white'  .
   #(1 2) -> 'red' } asDictionary
Kolb answered 5/6, 2015 at 14:23 Comment(0)
W
4

A Dictionary is a collection of Associations. It is, in fact, Smalltalk's canonical collection of Associations. (An instance of the Association Class is a key value pair, where the value can be an object of any Class).

The advantage a Dictionary gives you is that it has specialised methods for dealing with Associations, compared to other Collections you might be tempted to use.

A Dictionary provides:
removeKey: aKey . removes aKey
includesKey: aKey . checks for the existence of the key
includes: aValue . checks for the existence of a value
at:put: . shorthand for

anAssociation := Association key:value:   .
aDictionary add:    

e.g.

anAssociation := Association key:   'Hello' 
                             value: 'A greeting people often use' .
aDictionary add: anAssociation .

If the key already exists in the Dictionary, then at:put will overwrite the pre-existing value with the new value, so it's important to check and make sure that the key has a unique value when adding new items.

Both the key and the value can be an object instance of any Class. Every Association in a Dictionary can be any kind of object, and every single key and value might be a instance of a different Class of object from every other element in the Dictionary.

You can create an Association by

anAssociation := Association key: 'keyOfElement' value: 'valueOfElement'  

or, more succinctly,

anAssociation := 'keyOfElement' -> 'valueOfElement'  

If you want to use keys entirely made specifically of Symbols, there is also the Class IdentityDictionary

Wig answered 7/11, 2015 at 2:20 Comment(1)
n.b. { } asDictionary is not supported in Squeak, Dolphin Smalltalk or Amber. It is a Pharo-specific convenience syntax, and works from at least Pharo 1.4 upwards.Wig

© 2022 - 2024 — McMap. All rights reserved.