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:
| |
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:
| |
We cascade messages to the same object:
| |
Both messages go to Transcript. This is idiomatic Smalltalk.
Running with Docker
The easiest way to run Smalltalk without installing it locally:
| |
Running Locally
If you have GNU Smalltalk installed:
| |
On Ubuntu/Debian, install with:
| |
On macOS with Homebrew:
| |
Expected Output
Hello, World!
Alternative Approaches
Using printNl
For simple output, you can send printNl directly to a string:
| |
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:
| |
Each show: message appends to the output, and cr adds the newline.
Defining a Method
In a more structured program:
| |
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:
- Unary - No arguments:
5 negated,'hello' size - Binary - One argument, operator-like:
3 + 4,'a' , 'b' - 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:
| |
Double quotes are for comments, not strings!
Exploring the REPL
GNU Smalltalk provides an interactive environment:
| |
The REPL (Read-Eval-Print Loop) shows you the result of each expression.
Smalltalk vs Other Languages
Python:
| |
Ruby:
| |
JavaScript:
| |
Smalltalk:
| |
Notice how Smalltalk’s version reads almost like English: “Transcript, show this string, then carriage return.”
Understanding Message Precedence
Smalltalk has strict precedence rules:
- Unary messages first (left to right)
- Binary messages second (left to right)
- Keyword messages last (left to right)
Parentheses override this:
| |
Why show: and cr?
Smalltalk’s I/O is object-oriented:
Transcriptis a stream objectshow:converts its argument to a string and outputs itcroutputs 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
| |
Forgetting Colons in Keyword Messages
| |
Missing the Cascade
| |
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