4.7 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
Smalltalk minimal syntax demostration
Unknown author.
exampleWithNumber: x
"A method that illustrates every part of Smalltalk method syntax
except primitives. It has unary, binary, and keyword messages,
declares arguments and temporaries, accesses a global variable
(but not and instance variable), uses literals (array, character,
symbol, string, integer, float), uses the pseudo variables
true false, nil, self, and super, and has sequence, assignment,
return and cascade. It has both zero argument and one argument blocks."
|y|
true & false not & (nil isNil) ifFalse: [self halt].
y := self size + super size.
#($a #a "a" 1 1.0)
do: [:each | Transcript show: (each class name);
show: ' '].
^ x < y
Implementations
- Squeak
- Cuis-Smalltalk: fork of Squak; aims to be small and simple, so a single person can understand the system entirely.
- Pharo: fork of Squeak; quite unintuitive (they also avoid using 'Smalltalk' for some reason), limited learning resources.
- GNU Smalltalk: headless implementation, underdocumented (as anything GNU).
- ...
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),
followed by a newline using the cr
message (also sent to Transcript
).
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"
Factorial
Smalltalk already provides a method factorial
but that's cheating so we will implement it here:
| n res |
res := 1.
n := 5.
1 to: n do: [:i | res := res * i].
Transcript show: 'fact of ', (n asString), ' = ', (res asString); cr.
Homework: add this as a method by extending the Number
class.
TODO
Resources
- https://learnxinyminutes.com/smalltalk
- Smalltalk Cheatsheet
- The Cuis Book
- GNU Smalltalk User's Guide
- GNU Smalltalk tutorial
- The Cuneiform Tablets of 2015: paper describing how an Smalltalk-72 interpreter could be engraved in a rock for reliable future-proof preservation.