Contents Previous Next

2. Why Smalltalk is so strange? (or a brief history of Smalltalk)

Smalltalk introduces three big concept in the 1980:

  1. The concept of GUI as we know now and the concept of IDE, an Integrated Development Environment with a integrated Code Manager and Database.
  2. The concept of class/object and so on. Best, Smalltalk it is a pure Object Oriented language, so all the GUI, the IDE and the tools used for editing the code are written in Smalltalk
    Even the bytecode compiler, which translate our code in bytecodes is written in Smalltalk!
  3. Is the first pure dynamic language.
  4. It is funny to use!

Alan Kay wanted a language for ordinary people and he gets it. Smalltalk uses only 2-3 concept, is easy to use and understand, and incredible powerful. Smalltalk can be used and understood by children. This has the well-known cost: the speed is not so high, but RISC chip have solved this problem: for example, Squeak is used for multimedia application and it is quite fast.

2.1 Download Right Now!

For teaching, we must have a free Smalltalk to start with.

We have a wide choice, but I suggest to you Squeak (you can find it in Squeak main site) because it have a good large library, it is free and you can get it for Unix-X11, Ms-Windows, Mac, or your preferred PDA (!).

After you have downloaded your zip (or tar or other archive type) expand it in a directory (for instance called Squeak).

A Smalltalk implementation is composed of an image (binary code), a major source file, a second "changes" file. The image is called Virtual Image (VI) because is independent form the platform you use for running Smalltalk.

In Figure1 you can see a taste of Smalltalk. The green window is a Browser, the yellow window is a workspace, and finally you see an Inspector titled "SmallInteger: 3". As you see, every window with a different purpose has a different color.

Figure 1, A taste of Smalltalk

The Browser is your best friend, and it is used for exploring the class stored in Smalltalk. In the leftmost panel, you see list of Categories. used for grouping the classes. Then you see a list of classes (Object is selected) of the selected category. Then you see a list of protocols, used for grouping methods (the protocol for printing is selected). Finally we have selected the printOn: method we see in the panel below.

The Workspace is a window where you can type what do you like and then ask Smalltalk to execute it. In Figure2, you see the menu you can pop-up pressing the middle button of the mouse. Note: The exact mouse button can change depending of your environment (for example on Mac you have only one button...). Refer to the Squeak documentation for more information,

Figure 2, The Workspace

What do you can do with this menu?

In the following examples, I suggest you to print the results or inspect them.

2.2 Hello world in Smalltalk

We now will write our first Smalltalk code!! Open a Transcript and a Workspace, using the main menu: click in a empty area of the screen, then select the sub menu "open..." as in Figure 3.

Figure 3, The First Example

Please type in a workspace:

Transcript cr. 
Transcript show:'Hello World'.
Transcript cr. 

Now open the menu of the workspace and select do-it or press cmd-d

Note: The cmd key changes in respect of your O.S. and environment. If you are using windows or linux use alt-d. If you are using an Apple Machintosh, use Apple-d. We will refer to this key as cmd, as the Meta key on emacs.

What does this code? It simply prints a carriage return, the string "Hello World" and another carriage return.

Please observe these small things:

  1. In Smalltalk, all strings are enclosed in single quote as in 'Hello World'
  2. A double quote is used for comments: "This is a nice comment"
  3. The point . is used as a statement separator.
  4. The ; (semicolon) character has a special meaning, as we see beyond.

Okay, this look like when you go from Pascal to C, with all this oddities...but do not worry!

Now we will analyze our small "program" in Smalltalk in every line:

  1. Transcript is an object used to reference to the Transcript window. For the moment, I will trust to your intuitive notion of Object.
  2. cr is a message directed to the Transcript. You are just talking to the Transcript saying it a very small thing: "please print a carriage return"
  3. show:'Hello World' is a parameterized message where you pass to the Transcript what actually you want print!! Even if some concept are obscured, the simplicity of the language should be sufficient to understand it.

2.3 Big Numbers

Now we will see a small thing you cannot do in Java or C with the standard library. Type in & evaluate:

2 raisedTo:128 

we will obtain the right number:


Another small example is:

3 raisedTo: 32 =>  1853020188851841 

These numbers are LargePositiveIntegers objects, and the class comment says to use:

I represent positive integers of more than 30 bits (ie, >= 1073741824). These values are beyond the range of SmallInteger, and are encoded here as an array of 8-bit digits. [...]

So we can play with very big integers without problems!

In fact, in Smalltalk is quite easy to build dynamic and not-limited structure. For example, imagine you want build a small collection of three strings; you can do it with:

(OrderedCollection new) add:'first'; add:'second'; add:'thirdString'. 

You will use often OrderedCollection objects, instead of fixed Arrays. It is so easy to use dynamic structure, because the language provide a garbage collector and an easy framework for accessing to them, as we see in the next paragraph. The semicolon ';' i used for separating more messages sent to the same object. For instance, the first Transcript example can be re-written as:

Transcript  cr; show:'Hello World'; cr.

Let's see another example:

Java Version:

Foo f;
// A comment
f = new Foo("AString");

Smalltalk Version:

"A comment"
f := Foo new: 'AString';

We declare temp variables in vertical bars, without inserting the type of the object. Smalltalk is dynamically typed: you do not need to know the type of your object before you use it! The assign operator in Smalltalk con be written in ':=' or, under Squeak, using the special char <-. Under Squeak this char is typed using the "_" character.

The Smalltalk expressions are generally in the form "object message". They are evaluated from left to right!

For example, the Java expression:

int i;

will became:

i := 1+(5*6).

Smalltalk will reduce this expression in this way:

1+ (5*6) => 1+30 => 31 
If we write


we get:

1+5*6 => 6*6=>36

This seems a bit strange, but this evaluation are done in the same way for every expression. What about method with more than one parameter?

Look at this:

Java Version:

Foo f;

Will be written as:

f methodWith: 1 twoParam: 2.

2.4 Code Blocks

First of all, we will look a two different way of enumerating elements from a collection:

Java Version:

Enumeration list;
Apple elem;
  elem=(Apple) list.nextElement();
  //work wirh elem here

Smalltalk way:

aVector do:[:elem |
  "work with elem here"
]. "End of iteration "

And last, for doing small loop in Java you write:

for(int i=1; i<=10; i++) {
  System.out.println( Integer.toString(i) );

In Smalltalk you can write:

1 to:10 do:[:i| 
  Transcript show: (i asString). 

In this example we send the special method to:do: to the object 1. This means: 'Tell to 1 to go until 10 doing at every loop the code block I give you'. Simple, isn't it? The "code block" is enclosed in '[....]'. The to:do: sends a object (called i) in the block. It is a simple way of passing arguments. So, it is simple to do something like:

if(x>0) {
 x = x+1;
}else {

Will become:

(x>0) ifTrue:[ x:=x+1. ] ifFalse:[ x:=0 ].

We send to the result of the evaluation of '(x>0)' the message ifTrue:ifFalse.

You can have block without parameters, for example:

5 timesRepeat: [ 
 Transcript show: 'j'. 
Transcript cr.

Will print five j and then a carriage-return on the Transcript.

2.5 The while loop

In Java, you write:

int i=5;
while(i>1) {
 System.out.println( Integert.toString(i*2) );

In Smalltalk you can use:

[i >0] whileTrue:[ 
 Transcript show: ((i*2) asString) ; cr. 

You can try some more complex examples as yourself!

The Squeak IDE: going on...

For learning base usage of Squeak IDE, please refer to the read-me and to the documentation you find in the Squeak Web site or try this tutorial: (see [IDETutorial] on References (below))

Contents Previous Next