DEV Community

Pan Chasinga
Pan Chasinga

Posted on

Learn OOP with Smalltalk

Before there were C++ and Java, the first programming language that introduced the concept of Object-oriented Programming was Simula. However, the most pivotal one that made OOP the dominant programming model was Smalltalk, a language pioneered by the legendary Alan Kay. Smalltalk was born to honor the union of man and machine--the human-computer interaction (HCI). It has gone on to influence all the major OOP languages used today such as Objective-C, Java, Python, and Ruby.

The history of Smalltalk isn't the focus here, although it's pretty dramatic and full of twists and turns. I'd like to say that in the 80s it was at the forefront of all languages, but some missteps such as expensive licensing had contributed to its decreased adoptions.

Byte Magazine Cover

Talk to Me

In Smalltalk, everything is an object. Even classes are objects. And the only way to direct an object to execute is to send a message telling it what to do. That is, calling a method on a receiver.

3 Types of messages are Unary, Binary, and Keyword.

Unary Messages

An Unary message is similar to a single-parameter function call. This type of message consists of a message name and an operand. The objects are put before the message name. The following are examples of unary message:

Syntax Description
x sin "return the result of sin(x)"
Date tomorrow "answer a new instance of class Date"
1 asString "return the number 1 as a string"
"hi" show "print 'hi' to stdout"

In these examples x, Date, 1, and "hi" are the objects and sin, tomorrow, asString, and show are the message names.

Binary Messages

Binary messages are used to specify arithmetic, comparison, and logical operations. A binary message can be either one or two characters long and can contain any combination of the following special characters:

      `+ / \ * ~ < > = @ % | & ? !`
Enter fullscreen mode Exit fullscreen mode

The following are examples of Smalltalk expressions using binary messages:

Expression Description
a + b "returns the result of sum a and b"
a - b "returns the difference of a and b"
a >= b "compares to see if a is greater than or equal to b and returns either true or false"

The first example can also read as "the message + is sent to the object a with a parameter b."

Keyword Messages

A Keyword message is equivalent to a method call with two or more named arguments. Look at the following example, the name of the object to which the message is sent is written first, then the name of the message (or method name), and followed by the parameter to be passed.

anObject aMessage: parameter.

Enter fullscreen mode Exit fullscreen mode

The colon is a required part of the message name.

When there is more than one parameter, a message name must appear for each parameter.

For example:

lassie eat: "lasagne" drink: "milk".

Enter fullscreen mode Exit fullscreen mode


lassie is the receiving object
eat: is the first part of the message name
"lasagne" is passed toeat:
drink:is the second part of the message name
"milk"is passed todrink:`


In Smalltalk, functions are known as blocks. They are executable routines that can be passed around as first-class citizens.


| x y |
x := [:a | a + 1].
y := x value: 2.



Most Smalltalk statements are expressions, so in order to suppress the implicit return value and write a block that contains multiple statements, use . to end each statement as you would ; in many C-like languages.

Fits on a Napkin

You'll hear a few times that the Smalltalk language spec is very small you can write it on the back of a napkin.

In general, the lack of good tooling around the language and nice REPL that made it pretty difficult to adopt. Having to download a bulky environment to write Smalltalk code in is just too much friction. It could've been extremely fun if Smalltalk had a REPL like Ruby's.

Top comments (0)