Smalltalk

Smalltalk was created at Xerox PARC in the 1970s by Alan Kay and his team. It is object-oriented in the purest sense: numbers, booleans, classes, blocks of code and even the compiler are objects, and the only thing you ever do is send a message.

Paradigm: Object-Oriented Programming

What Makes Smalltalk Special

  • Everything is an object. 3, true, nil, a class and a code block are all objects. There are no primitive types to special-case.
  • Everything happens by message. Control flow is not syntax: ifTrue:ifFalse: is a message sent to a boolean, and timesRepeat: is a message sent to a number.
  • Tiny syntax. Three kinds of messages (unary, binary and keyword) and a handful of literals are the whole grammar. It famously fits on a postcard.
  • A living image. You program inside a running system. Browsers, inspectors and the debugger are part of the same environment, and you can change code while the program runs.
  • It shaped everything. Model-View-Controller, refactoring tools, unit testing (SUnit) and the overlapping-window desktop grew out of Smalltalk, and it influenced Java, Ruby and Objective-C.

Example: A counter class

Object subclass: Counter [
    | count |
    init [ count := 0 ]
    increment [ count := count + 1. ^self ]
    count [ ^count ]
]

| c |
c := Counter new init; yourself.
c increment; increment; increment.
c count printNl.
(c count > 2)
    ifTrue: ['big' displayNl]
    ifFalse: ['small' displayNl].

How It Works

  • The class is created by sending subclass: to Object. It has one instance variable, count, and three methods. This is GNU Smalltalk syntax; Pharo uses a different browser-based way to define classes.
  • Counter new creates an instance. The semicolon is a cascade: several messages sent to the same object.
  • c increment; increment; increment sends the message three times, so c count prints 3.
  • The last statement is not an if: ifTrue:ifFalse: is a message sent to the boolean result of c count > 2, and the two blocks are objects passed to it. It prints big.

History and Where It Is Used

Pharo and Squeak are the main modern Smalltalks, used for teaching, research and some business systems, and Cincom Smalltalk and GemStone still serve finance and logistics. Even if you never ship Smalltalk, learning it is the fastest way to understand what object-oriented design was meant to be.

Learn More