wiki/articles/smalltalk.md

3.2 KiB

Smalltalk

Smalltalk is a pure object oriented (not the "modern" variant), garbage collected and highly dynamic programming language created in Xerox PARC by a group of scientists, including Alan Kay. Smalltalk has a minimal syntax which is famously said to be able to "fit on a postcard"; building upon a minimal set of primitive objects. Everything is an object in this language.

Smalltalk is built upon the concept of message passing, where objects communicate between each other by sending messages, which can optionally carry a value. Unlike most OOP languages, an object can receive any message and its up to it whether to reply it or return an error if it doesn't understand the received message.

"Sends the message `say` to `anObject` with the value `hi`"
anObject say: 'hi'

Objects from a hierarchy, where ultimately all objects descend from the same Object object.

Being a highly dynamic language, Smalltalk is usually used in an IDE supporting this paradigm by using GUI to allow for easier inspection of the program, but "headless" implementations of Smalltalk exist.

In Smalltalk implementations that provide with an interactive environment, Smalltalk code is not saved exactly in a textual form like other programming languages; instead Smalltalk is more "image" oriented, with an image being able to contain the code of a program and its live state (similar to how some Lisps work).

Smalltalk revisions

  • Smalltalk-71: prototype design by Alan Kay
  • Smalltalk-72: first actual implementation, in BASIC.
  • Smalltalk-80: most popular standard
  • ANSI Smalltalk: released in 1998

Implementations

Examples

For now the examples that I will show here are run in GNU Smalltalk to keep things textual. GNU Smalltalk uses the .st extension for Smalltalk files.

Hello world

Here we send a string to the global Transcript object, which will output the message in the "transcript" window (on a GUI environment) or to stdout for a headless implementation (like GNU Smalltalk).

Transcript show: 'Hello, world!'; cr.

and run it with:

gst hello.st

Extending a class: String

We can also extend builtin classes such as String to add our own method:

'smalltalk' removeVowels.                         "Error: `String` doesn't understand this message"
String extend [
    removeVowels [
        ^ self reject: [:c | c isVowel]
    ]
].
Transcript show: 'smalltalk' removeVowels; cr.    "==> smlltlk"

TODO

Resources