If you'll remember from the beginning of the chapter,
we started out by saying:
Smalltalk at: #x put: 0 !
This code should look familiar--the at:put: message is how
we've been storing information in our own arrays and
dictionaries. In a Smalltalk environment the name Smalltalk
has been preset to point to a dictionary (21) which both you and
Smalltalk can use. To see how this sharing works, we'll
first try to use a variable which Smalltalk doesn't know
about:
y := 0 !
Smalltalk complains because y is an unknown variable.
Using our knowledge of dictionaries, and taking advantage of
our access to Smalltalk's dictionary, we can add it ourselves:
Smalltalk at: #y put: 0 !
The only mystery left is why we're using #y instead of our
usual quoted string. This is one of those simple questions
whose answer runs surprisingly deep. The quick answer is
that #y and 'y' are pretty much the same, except
that the former will always be the same object each time you use
it, whereas the latter can be a new string each time you do so.
(22)
Now that we've added y to Smalltalk's dictionary, we
try again:
y := 1 !
It works! Because you've added an entry for y, Smalltalk
is now perfectly happy to let you use this new variable.
If you have some spare time, you can print out the
entire Smalltalk dictionary with:
Smalltalk inspect !
As you might suspect, this will print out quite a large list
of names! If you get tired of watching Smalltalk grind it
out, use your interrupt key (control-C, usually) to bring
Smalltalk back to interactive mode.
Please take a moment to fill out
this visitor survey You can help support this site by
visiting the advertisers that sponsor it! (only once each, though)