![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
6.1.1 Starting up Smalltalk 6.1.2 Saying hello 6.1.3 What actually happened But how does it say hello? 6.1.4 Doing math Smalltalk too can do it! 6.1.5 Math in Smalltalk But in a peculiar way of course...
Assuming that GNU Smalltalk has been installed on your system, starting it is as simple as:
$ gst |
GNU Smalltalk ready
st>
|
You are now ready to try your hand at Smalltalk! By the way, when you're ready to quit, you exit Smalltalk by typing control-D on an empty line.
printNl is a upper case
N and a lower case L):
'Hello, world' printNl |
The front-line Smalltalk interpreter gathers all text until a '!' character and executes it. So the actual Smalltalk code executed was:
'Hello, world' printNl |
This code does two things. First, it creates an object of
type String which contains the characters "Hello, world".
Second, it sends the message named printNl to the object.
When the object is done processing the message, the code is
done and we get our prompt back.
You'll notice that we didn't say anything about printing
ing the string, even though that's in fact what happened.
This was very much on purpose: the code we typed in doesn't
know anything about printing strings. It knew how to get a
string object, and it knew how to send a message to that
object. That's the end of the story for the code we wrote.
But for fun, let's take a look at what happened when the
string object received the printNl message. The string object
then went to a table (22)
which lists the messages which strings can receive, and what code to
execute. It found that there is indeed an entry for
printNl in that table and ran this code. This code then walked through
its characters, printing each of them out to the terminal. (23)
The central point is that an object is entirely self-contained; only the object knew how to print itself out. When we want an object to print out, we ask the object itself to do the printing.
A similar piece of code prints numbers:
1234 printNl |
Notice how we used the same message, but have sent it to a
new type of object--an integer (from class Integer). The
way in which an integer is printed is much different from
the way a string is printed on the inside, but because we
are just sending a message, we do not have to be aware of
this. We tell it to printNl, and it prints itself out.
As a user of an object, we can thus usually send a particular
message and expect basically the same kind of behavior,
regardless of object's internal structure (for
instance, we have seen that sending printNl to an object
makes the object print itself). In later chapters we will
see a wide range of types of objects. Yet all of them can
be printed out the same way--with printNl.
White space is ignored, except as it separates words. This example could also have looked like:
1234 printNl |
However, GNU Smalltalk tries to execute each line by itself if possible. If you wanted to write the code on two lines, you might have written something like:
(1234
printNl)
|
From now on, we'll omit printNl since GNU Smalltalk
does the service of printing the answer for us.
An integer can be sent a number of messages in addition to just printing itself. An important set of messages for integers are the ones which do math:
9 + 7 |
Answers (correctly!) the value 16. The way that it does this, however, is a significant departure from a procedural language.
In this case, what happened was that the object 9 (an
Integer), received a + message with an argument of 7
(also an Integer). The + message for integers then caused
Smalltalk to create a new object 16 and return it as the
resultant object. This 16 object was then given the
printNl message, and printed 16 on the terminal.
Thus, math is not a special case in Smalltalk; it is
done, exactly like everything else, by creating objects, and
sending them messages. This may seem odd to the Smalltalk
novice, but this regularity turns out to be quite a boon:
once you've mastered just a few paradigms, all of the language
"falls into place". Before you go on to the next
chapter, make sure you try math involving * (multiplication),
- (subtraction), and / (division) also. These
examples should get you started:
8 * (4 / 2) 8 - (4 + 1) 5 + 4 2/3 + 7 2 + 3 * 4 2 + (3 * 4) |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |