Chapter 2: Introducing Smalltalk

Richard Kenneth Eng
Learn How To Program
7 min readJun 4, 2017

--

Smalltalk is many things. It’s a small, simple, reflective, dynamically typed, object-oriented programming language with first-class functions and lexical closures.

Smalltalk is also an IDE, or Integrated Development Environment, that supports live coding and debugging.

Smalltalk is a language virtual machine that supports image persistence, which means you can save and restore the execution state of an application at any time.

A “software Internet”

Smalltalk is also a philosophy of computer programming, one that is rooted in the idea of a “software Internet”: virtual computers universally connected through virtual networks. There are only objects (objects are made from objects, the network is made from objects, etc.). (No applications, no file system, just synergies of the virtual computers.) The language is the language of messages between objects.

See Basic Glossary before proceeding.

Prof Stef

To give you a taste of Smalltalk syntax, follow this interactive tutorial to the end (FYI, Amber is a flavour of Smalltalk). I’ll wait for you here…

Are you done? Prof Stef shows you the key elements of Smalltalk syntax. First of all, you need to understand that nearly all of the syntax is about sending messages to objects, and everything in Smalltalk is an object, including literal values such as numbers and strings. To recap, there are three kinds of messages: unary, binary, and keyword. For example:

Date today. "unary message, which requires no other information"1 + 3. "binary message, which sits between the receiver object and argument object"window alert: 'hello world!'. "keyword message, which takes one or more arguments"

By the way, in Smalltalk, comments are delineated by double quotes.

The precedence rule says unary messages are performed first, then binary, then keyword. Otherwise, operations are performed from left to right. This can make arithmetic look slightly odd:

5 + 3 * 2. "produces 16, not 11. You can force the normal precedence with parentheses:"
5 + (3 * 2). "a bit unusual but not a hardship"

Smalltalk also has conventional control structures such as loops and if statements; they’re just not built into the language. They use keyword messages. Otherwise, they look pretty much the same as in other languages. For example, here’s an if statement in several languages:

"Smalltalk if statement"
time > 120
ifTrue: [Transcript show: 'Time expired.'; cr.
time := 0]
ifFalse: [Transcript show: 'Time remaining: ',
(120 - time) printString, ' minutes'; cr]
# Python if statement
if time > 120:
print 'Time expired.'
time = 0
else:
print 'Time remaining: ', 120 - time, ' minutes'
/* C language if statement */
if (time > 120) {
printf("Time expired.\n");
time = 0;
}
else
printf("Time remaining: %i minutes\n", 120 - time);

(In Smalltalk, the comma operator performs string concatenation. The cr message inserts carriage return.)

In future, you may return to Prof Stef for a refresh of your understanding of Smalltalk syntax.

Smalltalk is dynamically typed. This means that you don’t have to declare any data type (such as integer or string) for a variable that you create. A variable is simply a placeholder for a data value. In the above example, ‘time’ is a variable that contains the number of minutes to expiry. A dynamically typed variable can hold a value of any data type.

(In a statically typed language, you must declare, or infer, a data type for any variable you create. Thereafter, this variable can only hold values of that particular data type. Static typing is rigid and using it is less flexible than dynamic typing, but it’s regarded as safer for coding.)

Smalltalk has first-class functions and closures. Prof Stef used the parlance of “blocks” for closures but you’ll find the previous technical terms commonly applied in other programming languages. (The really geeky term “lambdas” is often used, as well.)

Basically, a first-class function can be treated just like any other data value. It can be assigned to variables and passed as arguments to other functions. It can be a return value from a function. A closure is a special case where the function encloses its surrounding scope or environment; we don’t need to get into this here.

First-class functions are what make functional programming (FP) languages possible. Thus, Smalltalk can be a FP language too, though it lacks certain conveniences that you find in Clojure and Erlang.

The Pharo IDE

Different Smalltalk dialects have somewhat varying IDEs but they generally share common elements. We shall focus on the Pharo IDE.

When you click on the background, it brings up the World menu from which you can select any number of facilities:

Choose System Browser to bring up one of the most important tools**. The System Browser allows you to browse all the classes and methods in your Smalltalk system, including your own application. From here, you can create new classes, add new methods, write your entire program. It’s rather like Visual Studio or Eclipse, except much simpler and more elegant. The Browser consists of a number of panes:

The Packages pane shows groups of related classes known as packages. The Classes pane shows all the classes for a particular package. For a given class, the Protocols pane shows groups of related methods known as protocols; this is a matter of convenience and is not strictly required. The Methods pane shows all the methods for a particular protocol (or otherwise all the methods for the class).

The Editing pane is where you create or edit the source code. You can create new classes or methods by simply typing into the pane, or modifying an existing class or method, and then saving (using Ctrl-S). There is a total lack of ceremony; you just do whatever you like.

The Comments pane contains optional comments or documentation for your class. Its presence can be toggled on or off by clicking on the Comments button.

The Quality Assistant’s Feedback pane provides helpful hints regarding a class or method.

One of the major features of Smalltalk is its live coding and debugging capability. The late James Robertson provided an excellent video to show how to use the Pharo (Smalltalk) debugger:

[There is now a separate tutorial for How to use the Pharo Debugger.]

Playground and Transcript

Other useful tools include Playground and Transcript. Playground is a sort of scratchpad where you can experiment with snippets of code. It’s a more advanced form of the REPL (Read-Eval-Print Loop) that exists in many other languages.

A Transcript is like a system console where you can see diagnostic output (or “print statements”). It’s very useful for quick-and-dirty debugging (i.e., diagnosing what’s wrong with your program).

For more information about Pharo (Smalltalk) and its tools, reference the Pharo companion book, “Pharo by Example.”

Your first “Hello World” program

Let’s create our first simple little Pharo program. In the editing pane, you should see something like

Object subclass: #NameOfSubclass
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: ''

Replace ‘#NameOfSubclass’ with ‘#Hello’. Hello is the name of our program.

We put the Hello program in its own category. Let’s create a category named ‘Hello’. Just type ‘Hello’ in the category field. Save the changes by typing Ctrl-S.

Now we must create an initialization method for Hello to execute. It’s called ‘initialize’. Select ‘no messages’ in the Protocols pane to indicate that we’re creating a method. Then in the Editing pane, type the following:

initialize
Transcript show: 'Hello World'.

This method will print ‘Hello World’ to the Transcript window.

Finally, save the changes. A pop-up may ask for your name. Just type in whatever name you like.

To run our program, we need to execute ‘Hello new’ in the Playground (you can find it in the World menu). To see the ‘Hello World’ output, we need to open the Transcript window (it’s under ‘Tools’ in the World menu).

So when you do this:

Hello new

you should see ‘Hello World’ printed in the Transcript window. Congrats!

The Smalltalk Image

Smalltalk is a language virtual machine. A virtual machine is a program that emulates another computer system. This other computer system can simply be an abstract platform with no particular tie to hardware. Thus, a language virtual machine is a conceptually hardware-independent platform that can execute instructions (known as bytecode) generated by the language. Other languages that operate in a similar fashion include Java (and a whole host of languages that can run atop of the JVM), Erlang/Elixir, and Ruby. (Of course, a virtual machine’s implementation is hardware-specific.)

An image is the totality of the Smalltalk system (including application) in computer memory. Thus, it represents the complete execution state of a program. Smalltalk allows you to save the image onto disk in an image file. In this way, you can preserve (or persist) the execution state and resume execution of the program at a later time exactly from where you left off! I can’t tell you how awfully convenient this is for a programmer.

No other major programming language supports this kind of process, and unfortunately Java/Python/JavaScript/Ruby programmers don’t know what they’re missing.

Now that you are familiar with Smalltalk syntax and you have a head start on learning to use the Pharo IDE, we can begin looking at how to write an application.

** When you first open System Browser, you will be prompted in the Quality Assistant’s Feedback pane to enable sending out diagnostic and usage data: “No diagnostic and usage data is being sent. Would you like to send diagnostic and usage data to help us improve Pharo?” Just click on “Go to settings” and then close the Settings Browser. You don’t need to enable anything unless you want to.

Make sure you save changes when you quit Pharo so that you don’t get this prompt again.

--

--