path | title |
---|---|
/learnings/smalltalk |
Learnings: Smalltalk |
- Slack Syntax on a postcard
- 6.
#($a #a "a" 1 1.0).
- Smalltalk Philosophy
- Fun things to see
- Alternative implantations
- Idioms
- Learning Resources
1 exampleWithNumber: x anotherParameter: z
2 "A method that illustrates every part of Smalltalk method syntax
except primitives. It has unary, binary, and keyboard messages,
declares arguments and temporaries, accesses a global variable
(but not an 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."
3 | y |
4 true & false not & (nil isNil) ifFalse: [self halt].
5 y := self size + super size.
6 #($a #a "a" 1 1.0)
7 do: [ :each | | localVariableInAClosure |
8 Transcript show: (each class name);
show: ' '].
9 ^x < y
creates a new method, takes parameter x. Call it like:
exmpleWithNumber: 42 anotherParameter: 43.
“named keyword arguments” like Objective-C (Objective-C stole these from Smalltalk)
comments are in double quotes
declare local variables in the top of a section of code, in between pipe characters
very very few control structures in Smalltalk (even method definitions are actually method calls on objects!). For example, if statements are implemented as method calls on boolean
the [] nonsense is a Smalltalk block… aka closure / anon function. In smalltalk, in blocks, the last statement is returned as the result of the block
lines terminated by .
(not ;
)
:=
is the assignment operator, like Pascal. Here we’re calling the size
method on self
(aka this
), and the same method on super
aka the superclass this class inherits from
here we create an array. There’s two ways to do an array in Smalltalk, this is one of them
Space delimited item definitions
do
takes a closure. The closure will be passed one parameter (which we all each
here.). That closure declares one local variable, localVariableInAClosure
.
It looks kind of odd, but it is consistent...
The results of the last statement in a closure is the return value of that closure.
unlike a block, methods need to explicitly return something. the ^
is the return statement
(the awesome(??) thing about Smalltalk is that it’s image, not file based. Most Smalltalks integrate a window manager, code editor and saves state. Thus when you open Pharo Smalltalk you end up in essentially a desktop environment. Of course, we know that individual files for a program won out, but in Smalltalk the code lives in the image - so source control as we know is… there in Pharo, but more like a bridge then anything else but as long as you save your image both window positioning AND instance data for non-garbage collected objects are PERSISTED for you thus why I like it for a dev workbench - saving state is cheap, and everything lives together aka all my scripts are strewn out inside the image, not strewn out inside the file system
Billed as "Smalltalk foe those who can type", doesn’t include a UI or browser.
MIT backed implementation, the backend for a number of interesting projects (Scratch, Seaside.
Commercially supported Smalltalk. about $7,000 per license
Smalltalk image based... but on the web.
|s t|
t := Time now asDateAndTime .
s := String new writeStream.
s << t dayOfWeekName.
s << ','.
s contents.