The Smalltalk

Getting started

Programming in Smalltalk is based on objects (classes and their instances) communicating to each other via messages. Objects, messages and variables are the most important elements in Smalltalk syntax and they make the syntax surprisingly clean and simple.

The easiest way how to start with any programming language is to look at several simple examples and make sense of them, so here are two very simple pieces of Smalltalk code:

Example 1

|a|         "declare a as local variable"
a := 1.     "object 1 allocated and the reference to it stored to a"
a := a + 1. "message expression - explained later"
^ a.        "return: 2"

Exmaple 2

|a b|             "declare a b as local variables"
a := #(1,5,7).    "#(1,5,7) means an array of specified elements" 
b := a.           "make b pointing to the same object as a"
a at: 1 put: 2.   "change first element to 2"
^ b.              "return: #(2,5,7)"      

There are certain differences in the way of executing programs from implementation to implementation. In implementations with graphical user interface (like Smalltalk/V or Smalltalk/X) there is a window titled usually as "Workspace", where you can write your code and then using mouse select the lines you are going to execute. Pressing the right mouse button then you'll get a context menu, where you can choose to start execution. This way it works in Smalltalk/X, while in Smalltalk/V it's neccessary to press Control-S instead of the right mouse button.

Now let's discuss the programs mentioned above.

One of the biggest confuse on Smalltalk is the syntax, semantics and style of evaluation of message expressions and also the purpose and using of variables. These two elements are the basic keys to understanding the Smalltalk programming.

Variables in Smalltalk are untyped and they are treated as implicit pointers, what means that variables contain only references to objects and not the objects themselves (they're bound with an object. (Example 2 demonstrates this). Variables can be either global (the first letter is a capital) or local.

Message expressions Allmost all of the work in Smalltalk is performed by sending messages to objects. Objects on basis of the received message may perform some actions. For example the line a:=a+1. from the first example IS NOT an arithmetic expression, although it looks so. The semantics can be interpreted following way:

" Object bound with variable a (receiver) is sent a meessage + with additional argument 1"

Object-receiver has typicaly defined some reaction for particular message. In this case new object representing the result is created and this new object is bound with variable a.

For the things not to be so easy, there are 3 types of messages in Smalltalk

At this place it is important to mention something about the keyword messages. While binary message is in fact a message with one additional argument, keyword message can be treated as ONE MESSAGE with more than one additional argument. Be very careful not to take keyword message as a sequence of several separate messages.

There is still one another thing, which can look strange, especially if you are experienced in C++ and its priority mechanism.
The evaluation of message expressions starts with unary messages and continues with binary and then keyword messages. You can modify this default order by using parenthesis.
This can look strange, especially if you are experienced in C++ and it priority mechanism. Sometimes this concept leads to non-intuitive results, so again, it is neccessary to have all this in mind, when writting your own Smalltalk programs.