22 - Smalltalk#
Basics#
Object-Oriented#
Everything is an object.

GNU Smalltalk#
Interactive mode
gst
st> 4+5
9
st> 4+5*6
54
st> ObjectMemory quit
You can also type CTRL-D
to quit.
Literals#
Numbers:
12
,23.4
Characters:
$a
,$A
Strings:
'This is a string.'
Symbols (strings used for names):
#foo
Arrays:
#('three' 'four' 5 6 $Z)
Blocks:
[:x | x + 2]
Naming#
Names in Smalltalk are a sequence of letters and digits beginning with a letter.
Global variables, class variables, pool dictionaries, and class names should start with an uppercase letter. Instance variables, methods, block arguments, and temporary variables start with a lowercase letter.
Only 6 “keywords” are reserved: true
, false
, nil
, self
, super
, thisContext
.
Variables#
Temporary variables
Instance variables
Class variables
Pool variables: A pool variable is a variable whose scope is a defined subset of classes.
Global variables (
Smalltalk
dictionary)
Expression#
A variable name
A literal
A message expression
A block expression
Messages#
Unary, keyword, and binary messages
Message chaining, evaluated from left to right
Unary messages take precedence over binary messages
Binary messages take precedence over keyword messages
All binary messages have the same precedence
Parantheses change the precedence
Assignment Statement#
variable := expression
quantity := 19
index := initialIndex
index := index + 1
y := quantity sqrt
z := 1 + 2 * 3
f := [:x|x+1]
Collections#
Arrays
Sets
Dictionaries

Array#
x := Array new: 20
x at: 1
x at: 1 put: 99
Note that the first index is 1.
Set#
x := Set new
x add: 5. x add: 7. x add: 'foo'
x add: 5; add: 7; add: 'foo' "Message Cascading"
x remove: 5
x includes: 7
Dictionary#
y := Dictionary new
y at: 'name' put: 'John Smith'
y at: 'age' put: 25
y at: 'name'
y at: 'age'
Control Structures#
Selection#
aBoolean ifTrue: aBlock "evaluates aBlock if aBoolean is true"
aBoolean ifFalse: aBlock "evaluates aBlock if aBoolean is false"
aBoolean ifTrue: trueBlock ifFalse: falseBlock "evaluates trueBlock if aBoolean is true, falseBlock if false"
aBoolean ifFalse: falseBlock ifTrue: trueBlock "evaluates trueBlock if aBoolean is true, falseBlock if false"
index <= limit
ifTrue: [total := total + (list at: index)]
index <= limit
ifTrue: [total := total + (list at: index)]
ifFalse: []
Iteration#
aBooleanBlock whileTrue: loopBlock "as long as aBooleanBlock evaluates to true, loopBlock is evaluated"
aBooleanBlock whileFalse: loopBlock "as long as aBooleanBlock evaluates to false, loopBlock is evaluated"
aBooleanBlock whileTrue "repeats evaluating aBooleanBlock until it returns false"
aBooleanBlock whileFalse "repeats evaluating aBooleanBlock until it returns true"
index := 1.
[index <= list size]
whileTrue: [list at: index put: 0.
index := index + 1]
Multiple Iterations#
n timesRepeat: [
...
repeated statements
...
]
5 timesRepeat: [100 printNl]
Interval and Iteration#
| anArray |
anArray := #('one' 'deux' 'drei' 'quatro' 5 6.0).
1 to: 6 do: [:idx | (anArray at: idx) printNl].
Collection and Iteration#
This uses the following form: aCollection do: aOneArgBlock
.
| anArray |
anArray := #('one' 'deux' 'drei' 'quatro' 5 6.0).
anArray do:[:eachElement | eachElement printNl].
Collection Methods#
Detect:
#(4 7 10 3 17) detect: [ :each | each > 7 ]
Select, reject:
'now is the time' select: [ :each | each isVowel ]
Collect
#(1 2 3 4 5) collect: [ :i | i * i ]
Non-Interactive Mode#
"first.st"
"A program to print 'Hello World!' to the terminal."
'Hello World!' printNl
This can be run with:
gst first.st