| www.delorie.com/gnu/docs/smalltalk/gst_50.html | search |
![]() Buy GNU books! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 ! |
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:
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |