Smalltalk/Squeak Basics - Language

(Material provided by Rodney Walker at Georgia Tech)


The purpose of this document is to give a little intro to Smalltalk/Squeak, and allow you to jump in and play around with things.


Basic Usage: The Mouse

Smalltalk uses all three buttons of the mouse extensively. Within a Smalltalk window, the buttons work as follows: Also, you can left click on the greenish background of the Squeak window and get a system-wide menu with options to create new windows and to exit the systems.

Try it now. Left click on the background, hold the button down, select "open", and select "transcript". An orange window labeled "Transcript" should appear. Use the left button on the borders to shrink this window down a little, and then move it to the bottom of the screen out of the way. This window is where your output will appear later on.


Getting Started

Well, let's continue with what we started above. We'll keep things simple and open up "workspace" and "transcript" windows. You can think of these as being input and output windows. Here is where we will do some simple manipulations.

But first, let's get some basic language constructs down. We'll be using these later, so we might as well start with them. This will be easier if I compare across languages - Smalltalk vs C++ & Java.


Language Comparison Table - Smalltalk vs C/C++ vs Java
What Smalltalk C & C++ Java
Importing Libraries N/A #include < library_name.h > import java.library.*
End of line
End of command
Period Semicolon Semicolon
Comments "Comment" /* Comment */
// Comment
// Comment
Variable Declaration | a b c d | int a, b, c, d; int a, b, c, d;
Printing Transcript show: 'Text'. printf("Text");
cout << "Text";
System.out.println("Text");
Printing - variables Transcript show: (a) printString. printf("%d", a);
cout << a;
System.out.println(a);
Printing - newline Transcript show: 'Text'; cr. printf("Text\n");
cout << "Text" << endl;
System.out.println("Text");
(Automatic with println)
Compiling N/A make
gcc (or fave compiler)
javac file.java
Running Select Text, left-click, 'do it' a.out java file
Assignment a := 4
a _ 4 (will become )
a = 4 a = 4
Equality a = b (single "=") a == b (double "= =") a == b (double "= =")
Block Delineators Brackets - [ code ] Curly Braces - { code } Curly Braces - { code }
Object Creation obj := Object new
obj := Object initialize
obj := Object new initialize
Object obj obj = new Object()
Function - Usage/Call Method: value Function(value) Function(value)
Function - Types pass by value pass by value
pass by reference
pass by value?
Function - Return Value ^value (will become ) return value return value
Function - Self Reference use "self" use "this" use "this"
Terminology method function function


Other Basics

There are a couple of things to remember about Smalltalk:


Conditionals

These are done a little differently in Squeak. Although they could've been shown in the table above, I feel a little more explanantion is needed for them. I'll look at them in terms of usage. As in C/C++ and Java, these execute using code in blocks. Don't forget to put these inside brackets - []. And as in these other languages, you can have multiple statements in a block. Unlike with C/C++ and Java, you need to put a period after the braces when a block closes. Smalltalk treats a block as a statement.

While
A while loop from C/C++ would be done in the following way, using Smalltalk (there is also a "whileFalse:" construct) :

x := 0.
[ (x < 10) ] 
whileTrue: [Transcript show: (x) printString.
            Transcript show: ' '.
            x := x + 1.].


0 1 2 3 4 5 6 7 8 9 

As with other languages, you can build pretty much any other conditionals out of while loops and some combination of other code.

If/Else
There is a relatively easy way to do this as well. The important functions here are called ifTrue and ifFalse. They can be used separately or together to form an if/else block. If used together, put a period after the second block, but not the first one (notice in example below):

a := 0.
(a = 0) 
ifTrue:  [Transcript show: 'A is True'; cr.].

b := 0.
(b > 0)
ifFalse: [Transcript show: 'B is false'; cr.].

(a = b)
ifTrue:  [Transcript show: 'A and B are equal'; cr.]
ifFalse: [Transcript show: 'A and B are NOT equal'; cr.].


A is True
B is false
A and B are equal

For
Squeak has a cool way of working with this called to:by:do:

1 to: 10 by: 2 do: [:x | Transcript show: 'Hello '.].


Hello Hello Hello Hello Hello
Notice the ":x" in the block. This keeps track of the variable and the value for it. In some cases, you may want to know what this value is:
1 to: 10 by: 2 do: [:x | Transcript show: (x) printString.
                         Transcript show: ' '.].


1 3 5 7 9
You can also get rid of the by: and just do to:do: like this
1 to: 10 do: [:x | Transcript show: 'Hello '.].


Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello 
If you know exactly how many times you want to do something, then you can do that as well:
10 timesRepeat: [Transcript show: 'Hello '.].


Hello Hello Hello Hello Hello Hello Hello Hello Hello Hello