Smalltalk Language Basics Book Notes
13 Jun 2020
Smalltalk books are a great way to learn Object-oriented programming, because it helps rewire how you think about objects. Currently recapping my object-oriented programming knowledege with the help of Smalltalk An Introduction to Application Development using VisualWorks.
- Numbers: 8, 12.7, -0.0007, 8r177, 16r5E, 2r10001, 2.23e5, 2.23f
- Characters: $D, $+, $9, $$, $x
- Strings: 'test string' 'string with "embedded single quote'
- Symbols: #initialize #W80, #temp, #'3x'
- Literal Arrays: #(40 41 42), #((1 '2') ('first' #second), #(1 'one' (#5 'five'))
- Byte Arrays: #[1 2 3 0], #[16rFF 0 2r111 2e2]
- Variables: someObject, Integer, Smalltalk, temp3
- Pseudo-variables (object that can't be changed): nil, true, false
- Messages: <receiver><unary, binary or keyword message>
- Unary Messages: 3 negated, 100 factorial, Window new, anArray size, theta sin, 4 even
- Binary Messages: +, =, & (logical and), <=, , (string cocat), -, <, >=, | (logical or), /, >, // (integer division), *, ~=
- Binary message examples: 2 + 3, true & false, 3 * 4, 100 // 7, index -1
- Keyword messages: anArray copyFrom: 1 to: 3, index max: limit, anArray at: first put: 'aardvark'
The order of evaluation in a Smalltalk statement is:
- Unary messages left to right
- Binary messages left to right
- Keyword messages left to right
Round brackets can be used to alter the order of precedence: a + (b*2), (anArray at: 14) even, frame scale: (factor max: 5)
The assignment expression can be used to assign an object to a variabel. it looks like:
Some more examples:
newIndex := 1 this := that someString := 'Part Two' anArray := #(1 2 3 4 5)
Assignment expressions return values, so several assignments can be made together:
this := that := theOther stop := end := 0
When it has finished evaluating, every Smalltalk method returns an object to the sender of the message. If no object is specified, the receiver itself is returned.
If some other object should be returned, the '^' symbol is used to return the object and terminate the evaluation of the current method (though it need not necessarily be at the end of the message sequence). Any object can be returned this way.
A block represents a deferred sequence of messages. A block is represented by a sequence of expressions (seperated by full stops), enclosed by square brackets:
aBlock := [index + 2] aBlock := [anArray at: newIndex put: 2]
The expressions in a block are evaluated when the block receives the message value:
Blocks are used to implement control structures:
aNumber even ifTrue: [aString := 'even'] ifFalse: [aString := 'odd'] [index > 0] whileTrue: [index := index 1]
Variables part 2
Instance variables are only available to the specific object in which they are contained. They're simply names for pointers to other objects and always begin life as nil. Each instance of a class keeps its own internal copy of each instance variable.
Other types of variables:
- Temporary: exist only for the duration of some activity, like the evaluation of a method.
- Class Variables: are shared by all instances of a single class.
- Global variables: are shared by all instances of all classes (i.e. all objects). Try not to use these except for experimentation.
- Pool Variables: are shared by all instances of some classes, and are a way of creating common storage between object classes. These kinds of variables are rarely used.
- Comments: Text that is only for documentation purposes are enclosed in "double quotes".