|
5.2 Using some of the Smalltalk classesThis 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:
Now let's create some new objects.
5.2.1 An array in SmalltalkAn 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):
The For instance:
The slots of an array are initially set to "nothing" (which
Smalltalk calls
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
The second operation,
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 There is quite a bit of sanity checking built into an array. The request
Finally, note that the object stored in an array is just like any other object, so we can do things like:
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
The kind of object is printed out (i.e.,
Now let's toss some stuff into it. We'll add the numbers 5 and 7, plus the string 'foo'. We could type:
But let's save a little typing by using a Smalltalk shorthand:
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
we'll see that it now contains our data:
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:
We've added 5 several times, but when we printed our set back out, we just see:
What you put into a set with
The set now prints as:
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:
From which we see that x does indeed contain 7, but not 5.
Notice that the answer is printed as 5.2.3 DictionariesA 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:
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 Now we can map each key to a value:
which prints respectively:
We can also ask a dictionary to print itself:
which prints:
where the first member of each pair is the key, and the second the value. 5.2.4 Smalltalk dictionaryIf you'll remember from the beginning of the chapter, we started out by saying:
This code should look familiar--the
Smalltalk complains because
The only mystery left is why we're using
Now that we've added
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:
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 thoughtsYou'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. |