|
5.1 Getting started
5.1.1 Starting up SmalltalkAssuming that GNU Smalltalk has been installed on your system, starting it is as simple as:
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. 5.1.2 Saying helloAn initial exercise is to make Smalltalk say "hello" to you. Type in the following line (printNl is a upper case
N and a lower case L):
5.1.3 What actually happenedThe front-line Smalltalk interpreter gathers all text until a '!' character and executes it. So the actual Smalltalk code executed was:
This code does two things. First, it creates an object of
type
But for fun, let's take a look at what happened when the
string object received the 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. 5.1.4 Doing mathA similar piece of code prints numbers:
Notice how we used the same message, but have sent it to a
new type of object--an integer (from class
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 White space is ignored, except as it separates words. This example could also have looked like:
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:
Answers (correctly!) the value 16. The way that it does this, however, is a significant departure from a procedural language. 5.1.5 Math in Smalltalk
In this case, what happened was that the object
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
|