www.delorie.com/gnu/docs/smalltalk/gst_45.html   search  
Buy GNU books!

GNU Smalltalk User's Guide

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1.3 What actually happened

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 (17) 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. (18)

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.

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003