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, andtimesRepeat: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:toObject. 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 newcreates an instance. The semicolon is a cascade: several messages sent to the same object.c increment; increment; incrementsends the message three times, soc countprints3.- The last statement is not an
if:ifTrue:ifFalse:is a message sent to the boolean result ofc count > 2, and the two blocks are objects passed to it. It printsbig.
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.