January 22, 2020



Dictionary is a subclass of Set. Sets can store references to any kind of object. A dictionary, that is an instance of the Dictionary class, is a set of associations, by that I mean a collection of objects that are all instances of the class Association.
An association is a pair of associated objects, a key and a value. In Smalltalk both key and value are instance variables but it is important to remember that a key is an attribute that uniquely identifies an object. A good example of a key in our real world scenario would be a stock number. The value of the corresponding key could be a string representing the title of a painting. Here is an example of an association:

'5126' -> 'Window 1969'

-> is inherited from Object and any object that receives this message will answer with an association consisting of the receiver as the key and the argument as the value.
The above could have been written

Association key: '5126' value: 'Window 1969'

A key can be any object, but usually a string or a symbol is used. Symbols are better due to the speed with which they can be accessed. This will be discussed later when looking at the Symbol class.

To retrieve either the key or the value we can send the correspondingly named messages key or value to the association instance. So for example

anAssociation := Association key: '5126' value: 'Window 1969'.
aKey := anAssociation key

aKey references '5126'

Let us suppose that the gallery wishes to use a dictionary, one for each artist exhibiting in the show, in order to store associations that pair a stock number with a title. Here is one of the dictionaries.

fbaconDictionary := Dictionary new.
fbaconDictionary add: '4689'->'Meat Rack';
                 add: '5005'->'George smoking';
                 add: '4767'->'Man with a drink';
                 add: '5313'->'Another man with a drink'

This will answer with the last element added

'5313'->'Another man with a drink'

and the instance variable tally of the dictionary will reference 4.

Next page » Protocol of Dictionary

Previous page « Protocol of Set
































































Up to top of page