Beginner

Hello World in Smalltalk

Your first Smalltalk program - the classic Hello World example with Docker setup

Every programming journey starts with Hello World. Let’s write our first Smalltalk program and experience the language that defined object-oriented programming.

The Code

Create a file named hello.st:

1
Transcript show: 'Hello, World!'; cr

Understanding the Code

This single line contains the essence of Smalltalk - message passing to objects:

  • Transcript - A global object representing the output stream (like stdout). It’s where textual output goes.
  • show: - A message sent to Transcript asking it to display a string. The colon indicates this message takes an argument.
  • 'Hello, World!' - A string literal (single quotes in Smalltalk).
  • ; - The message cascade operator. It sends another message to the same receiver (Transcript).
  • cr - Short for “carriage return” - outputs a newline.

Message Cascade

The semicolon (;) is powerful. Instead of writing:

1
2
Transcript show: 'Hello, World!'.
Transcript cr

We cascade messages to the same object:

1
Transcript show: 'Hello, World!'; cr

Both messages go to Transcript. This is idiomatic Smalltalk.

Running with Docker

The easiest way to run Smalltalk without installing it locally:

1
2
3
4
5
# Pull the GNU Smalltalk Docker image
docker pull sl4m/gnu-smalltalk:latest

# Run the program
docker run --rm -v $(pwd):/app -w /app sl4m/gnu-smalltalk gst hello.st

Running Locally

If you have GNU Smalltalk installed:

1
2
# Run the script
gst hello.st

On Ubuntu/Debian, install with:

1
sudo apt-get install gnu-smalltalk

On macOS with Homebrew:

1
brew install gnu-smalltalk

Expected Output

Hello, World!

Alternative Approaches

Using printNl

For simple output, you can send printNl directly to a string:

1
'Hello, World!' printNl

This sends the printNl message to the string object, which prints itself followed by a newline.

Using Transcript with Multiple Messages

For more complex output:

1
2
3
4
Transcript
    show: 'Hello, ';
    show: 'World!';
    cr

Each show: message appends to the output, and cr adds the newline.

Defining a Method

In a more structured program:

1
2
3
4
5
6
7
Object subclass: Greeter [
    greet [
        Transcript show: 'Hello, World!'; cr
    ]
]

Greeter new greet

This defines a class Greeter with a method greet, then creates an instance and sends it the greet message.

Key Concepts

Everything is a Message

In Smalltalk, there are three kinds of messages:

  1. Unary - No arguments: 5 negated, 'hello' size
  2. Binary - One argument, operator-like: 3 + 4, 'a' , 'b'
  3. Keyword - Named arguments: array at: 1 put: 'x'

Our show: is a keyword message (note the colon after show).

Objects All The Way Down

Transcript is an object. 'Hello, World!' is an object (a String). Even cr returns an object. Smalltalk has no primitives - everything is an object responding to messages.

Single Quotes for Strings

Unlike many languages, Smalltalk uses single quotes for strings:

1
2
'This is a string'
"This is a comment"

Double quotes are for comments, not strings!

Exploring the REPL

GNU Smalltalk provides an interactive environment:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ gst
GNU Smalltalk ready

st> 'Hello, World!' printNl
Hello, World!
'Hello, World!'
st> 3 + 4
7
st> 'Smalltalk' size
9
st> quit

The REPL (Read-Eval-Print Loop) shows you the result of each expression.

Smalltalk vs Other Languages

Python:

1
print("Hello, World!")

Ruby:

1
puts "Hello, World!"

JavaScript:

1
console.log("Hello, World!");

Smalltalk:

1
Transcript show: 'Hello, World!'; cr

Notice how Smalltalk’s version reads almost like English: “Transcript, show this string, then carriage return.”

Understanding Message Precedence

Smalltalk has strict precedence rules:

  1. Unary messages first (left to right)
  2. Binary messages second (left to right)
  3. Keyword messages last (left to right)

Parentheses override this:

1
2
3 + 4 * 5     "Evaluates as 35, not 23! (7 * 5)"
3 + (4 * 5)   "Evaluates as 23"

Why show: and cr?

Smalltalk’s I/O is object-oriented:

  • Transcript is a stream object
  • show: converts its argument to a string and outputs it
  • cr outputs a carriage return (newline)

This is more flexible than a simple print function. You can:

  • Send to different streams
  • Chain multiple outputs
  • Format output however you want

GNU Smalltalk

Our Docker image uses GNU Smalltalk, which:

  • Runs from the command line (no GUI required)
  • Handles script files like hello.st
  • Follows traditional Smalltalk-80 semantics
  • Is free and open source (GPL)
  • Works on Linux, macOS, and Windows

Other popular Smalltalks (Pharo, Squeak) focus on graphical, image-based environments. GNU Smalltalk is better suited for scripts and command-line usage.

Historical Note

When Alan Kay’s team created Smalltalk at Xerox PARC in the 1970s, they envisioned computing as accessible to everyone, including children. The syntax was designed to be readable and consistent.

Sending show: 'Hello, World!' to Transcript demonstrates this philosophy: you’re politely asking an object to do something, describing what you want done, not how to do it.

Common Mistakes

Using Double Quotes for Strings

1
2
"Wrong - this is a comment!"
'Right - this is a string!'

Forgetting Colons in Keyword Messages

1
2
Transcript show 'Hello'     "Error - missing colon"
Transcript show: 'Hello'    "Correct"

Missing the Cascade

1
2
Transcript show: 'Hello, World!' cr    "Error - cr goes to the string!"
Transcript show: 'Hello, World!'; cr   "Correct - cr goes to Transcript"

Next Steps

Continue exploring Smalltalk’s elegant design:

  • Variables and data types (Numbers, Strings, Symbols, Arrays)
  • Control structures (ifTrue:ifFalse:, whileTrue:)
  • Blocks (Smalltalk’s closures)
  • Collections and iteration
  • Classes and inheritance
  • The image and development environment

Smalltalk rewards exploration. Its consistent object model means that once you understand message passing, you understand the whole language. The rest is just discovering which messages objects respond to.

Welcome to Smalltalk - the language that defined object-oriented programming!

Running Today

All examples can be run using Docker:

docker pull sl4m/gnu-smalltalk:latest
Last updated: