articles/smalltalk.md: more information, code examples

This commit is contained in:
tocariimaa 2025-03-08 15:37:23 -03:00
parent 50d8849913
commit 1cf8c29c2d

View file

@ -1,21 +1,65 @@
# Smalltalk
Smalltalk is a pure [object oriented](oop.md) (not the "modern" variant) and highly dynamic programming language created in Xerox PARC
by a group of scientists, including Alan Kay. Smalltalk builds upon a minimal set of primitive objects.
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.
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'
```
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).
TODO
## Implementations
- [Squeak](https://squeak.org/)
- [Pharo](https://pharo.org/)
- [Pharo](https://pharo.org/): fork of Squeak
- [GNU Smalltalk](https://www.gnu.org/software/smalltalk/): headless implementation
- ...
## 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).
```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])
]
]
'smalltalk' removeVowels. "==> smlltlk"
```
TODO
## Resources
- <https://learnxinyminutes.com/smalltalk>
- [Smalltalk Cheatsheet](https://angelfire.com/tx4/cus/notes/smalltalk.html)
- [GNU Smalltalk User's Guide](https://www.gnu.org/software/smalltalk/manual/)
- [*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.
paper describing how an Smalltalk-72 interpreter could be engraved in a rock for reliable future-proof preservation.