Back: Math in Smalltalk
Up: Tutorial
Forward: Arrays
 
Top: GNU Smalltalk User's Guide
Contents: Table of Contents
Index: Class index
About: About this document

5.2 Using some of the Smalltalk classes

This chapter has examples which need a place to hold the objects they create. The following line creates such a place; for now, treat it as magic. At the end of the chapter we will revisit it with an explanation. Type in:
 
   Smalltalk at: #x put: 0 !

Now let's create some new objects.

5.2.1 An array in Smalltalk  
5.2.2 A set in Smalltalk  
5.2.3 Dictionaries  Getting more sophisticated, eh?
5.2.4 Smalltalk dictionary  A very important dictionary, really
5.2.5 Closing thoughts  There always ought to be some closing thoughts


5.2.1 An array in Smalltalk

An array in Smalltalk is similar to an array in any other language, although the syntax may seem peculiar at first. To create an array with room for 20 elements, do(16):
 
   x := Array new: 20 !

The Array new: 20 creates the array; the x := part connects the name x with the object. Until you assign something else to x, you can refer to this array by the name x. Changing elements of the array is not done using the := operator; this operator is used only to bind names to objects. In fact, you never modify data structures; instead, you send a message to the object, and it will modify itself.

For instance:
 
   (x at: 1) printNl !
which prints:
 
   nil

The slots of an array are initially set to "nothing" (which Smalltalk calls nil). Let's set the first slot to the number 99:
 
   x at: 1 put: 99 !
and now make sure the 99 is actually there:
 
   (x at: 1) printNl !
which then prints out:
 
   99

These examples show how to manipulate an array. They also show the standard way in which messages are passed arguments ments. In most cases, if a message takes an argument, its name will end with `:'.(17)

So when we said x at: 1 we were sending a message to whatever object was currently bound to x with an argument of 1. For an array, this results in the first slot of the array being returned.

The second operation, x at: 1 put: 99 is a message with two arguments. It tells the array to place the second argument (99) in the slot specified by the first (1). Thus, when we re-examine the first slot, it does indeed now contain 99.

There is a shorthand for describing the messages you send to objects. You just run the message names together. So we would say that our array accepts both the at: and at:put: messages.

There is quite a bit of sanity checking built into an array. The request
 
   6 at: 1
fails with an error; 6 is an integer, and can't be indexed. Further,
 
   x at: 21
fails with an error, because the array we created only has room for 20 objects.

Finally, note that the object stored in an array is just like any other object, so we can do things like:
 
   ((x at: 1) + 1) printNl !
which (assuming you've been typing in the examples) will print 100.


5.2.2 A set in Smalltalk

We're done with the array we've been using, so we'll assign something new to our x variable. Note that we don't need to do anything special about the old array: the fact that nobody is using it any more will be automatically detected, and the memory reclaimed. This is known as garbage collection and it is generally done when Smalltalk finds that it is running low on memory. So, to get our new object, simply do:
 
   x := Set new !
which creates an empty set. To view its contents, do:
 
   x printNl !

The kind of object is printed out (i.e., Set), and then the members are listed within parenthesis. Since it's empty, we see:
 
   Set ()

Now let's toss some stuff into it. We'll add the numbers 5 and 7, plus the string 'foo'. We could type:

 
   x add: 5 !
   x add: 7 !
   x add: 'foo' !

But let's save a little typing by using a Smalltalk shorthand:

 
   x add: 5; add: 7; add: 'foo' !

This line does exactly what the previous example's three lines did. The trick is that the semicolon operator causes the message to be sent to the same object as the last message sent. So saying ; add: 7 is the same as saying x add: 7, because x was the last thing a message was sent to. This may not seem like such a big savings, but compare the ease when your variable is named aVeryLongVariableName instead of just x! We'll revisit some other occasions where ; saves you trouble, but for now let's continue with our set. Type either version of the example, and make sure that we've added 5, 7, and "foo":
 
   x printNl !

we'll see that it now contains our data:
 
   Set (5 'foo' 7)

What if we add something twice? No problem--it just stays in the set. So a set is like a big checklist--either it's in there, or it isn't. To wit:
 
   x add:5; add: 5; add: 5; add: 5 !
   x printNl !

We've added 5 several times, but when we printed our set back out, we just see:
 
   Set (5 'foo' 7)

What you put into a set with add:, you can take out with remove:. Try:

 
   x remove: 5 !
   x printNl !

The set now prints as:
 
   Set ('foo' 7)

The "5" is indeed gone from the set.

We'll finish up with one more of the many things you can do with a set--checking for membership. Try:
 
   (x includes: 7) printNl !
   (x includes: 5) printNl !

From which we see that x does indeed contain 7, but not 5. Notice that the answer is printed as true or false. Once again, the thing returned is an object--in this case, an object known as a boolean. We'll look at the use of booleans later, but for now we'll just say that booleans are nothing more than objects which can only either be true or false--nothing else. So they're very useful for answers to yes or no questions, like the ones we just posed. Let's take a look at just one more kind of data structure:


5.2.3 Dictionaries

A dictionary is a special kind of collection. With a regular array, you must index it with integers. With dictionaries, you can index it with any object at all. Dictionaries thus provide a very powerful way of correlating one piece of information to another. Their only downside is that they are somewhat less efficient than simple arrays. Try the following:
 
   x := Dictionary new.
   x at: 'One' put: 1 !
   x at: 'Two' put: 2 !
   x at: 1 put: 'One' !
   x at: 2 put: 'Two' !

This fills our dictionary in with some data. The data is actually stored in pairs of key and value (the key is what you give to at:---it specifies a slot; the value is what is actually stored at that slot). Notice how we were able to specify not only integers but also strings as both the key and the value. In fact, we can use any kind of object we want as either--the dictionary doesn't care.

Now we can map each key to a value:
 
   (x at: 1) printNl !
   (x at: 'Two') printNl !

which prints respectively:
 
   'One'
   2

We can also ask a dictionary to print itself:
 
   x printNl !

which prints:
 
   Dictionary (1->'One' 2->'Two' 'One'->1 'Two'->2 )

where the first member of each pair is the key, and the second the value.


5.2.4 Smalltalk dictionary

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 (18) 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. (19)

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 printNl !

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.


5.2.5 Closing thoughts

You've seen how Smalltalk provides you with some very powerful data structures. You've also seen how Smalltalk itself uses these same facilities to implement the language. But this is only the tip of the iceberg--Smalltalk is much more than a collection of "neat" facilities to use. The objects and methods which are automatically available are only the beginning of the foundation on which you build your programs--Smalltalk allows you to add your own objects and methods into the system, and then use them along with everything else. The art of programming in Smalltalk is the art of looking at your problems in terms of objects, using the existing object types to good effect, and enhancing Smalltalk with new types of objects. Now that you've been exposed to the basics of Smalltalk manipulation, we can begin to look at this object-oriented technique of programming.




This document was generated on May, 12 2002 using texi2html