Next: , Previous: , Up: Some classes  


6.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

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’. This is also the first example where we’re using more than one statement, and thus a good place to present the statement separator—the . period:

   x add: 5. x add: 7. x add: 'foo'

Like Pascal, and unlike C, statements are separated rather than terminated. Thus you need only use a . when you have finished one statement and are starting another. This is why our last statement, ^r, does not have a . following. Once again like Pascal, however, Smalltalk won’t complain if your enter a spurious statement separator after the last statement.

However, we can save a little typing by using a Smalltalk shorthand:

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

This line does exactly what the previous one 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

we’ll see that it now contains our data:

   Set ('foo' 5 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; yourself

We’ve added 5 several times, but when we printed our set back out, we just see:

   Set ('foo' 5 7)

yourself is commonly sent at the end of the cascade, if what you are interested in is the object itself—in this case, we were not interested in the return value of add: 5, which happens to be 5 simply. There’s nothing magic in yourself; it is a unary message like printNl, which does nothing but returning the object itself. So you can do this too:

   x yourself

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
   x includes: 5

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:


Next: , Previous: , Up: Some classes