wiki/articles/smalltalk.md

108 lines
4.7 KiB
Markdown

# Smalltalk
Smalltalk is a pure [object oriented](oop.md) (not the "modern" variant), [garbage collected](garbage_collection.md)
and highly dynamic [programming language](programming_language.md) created in Xerox PARC by a group of scientists, including Alan Kay.
Smalltalk has a [minimal](minimalism.md) [syntax](syntax.md) 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.
```smalltalk
"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](ide.md) supporting this paradigm by using
[GUI](gui.md) 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](lisp.md) work).
## Smalltalk revisions
- Smalltalk-71: prototype design by Alan Kay
- Smalltalk-72: first actual implementation, in [BASIC](basic.md).
- Smalltalk-80: most popular standard
- ANSI Smalltalk: released in 1998
## Smalltalk minimal syntax demostration
Unknown author.
```smalltalk
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](https://squeak.org/)
- [Cuis-Smalltalk](https://cuis.st/): fork of Squak; aims to be small and simple, so a single person can understand the system entirely.
- [Pharo](https://pharo.org/): fork of Squeak; quite unintuitive (they also avoid using 'Smalltalk' for some reason), limited learning resources.
- [GNU Smalltalk](https://www.gnu.org/software/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`).
```smalltalk
Transcript show: 'Hello, world!'; cr.
```
and run it with:
```sh
gst hello.st
```
### Extending a class: `String`
We can also extend builtin classes such as `String` to add our own method:
```smalltalk
'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:
```smalltalk
| 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](https://angelfire.com/tx4/cus/notes/smalltalk.html)
- [The Cuis Book](https://cuis-smalltalk.github.io/TheCuisBook/)
- [GNU Smalltalk User's Guide](https://www.gnu.org/software/smalltalk/manual/)
- [GNU Smalltalk tutorial](https://www.gnu.org/software/smalltalk/manual/html_node/Tutorial.html)
- [*The Cuneiform Tablets of 2015*](https://tinlizzie.org/VPRIPapers/tr2015004_cuneiform.pdf):
paper describing how an Smalltalk-72 interpreter could be engraved in a rock for reliable future-proof preservation.