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.

Language Basics

Literals

Parsing Rules

The order of evaluation in a Smalltalk statement is:

  1. Unary messages left to right
  2. Binary messages left to right
  3. 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)

Assignments

The assignment expression can be used to assign an object to a variabel. it looks like:

<variable>:=<message send>

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

Returning

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.

Blocks

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:

aBlock 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: