Edit Rename Upload Download Back to Top

notation used in Niall Ross' Smalltalk examples

Block notation is described here. The following very briefly summarises the rest of the notation used in my code examples.

1) When writing Smalltalk textual examples, the usual symbol to express that a class has a method is >>

   MyClass>>myMethod
      someCode

means MyClass has the method named myMethod whose implementation is shown by someCode. This symbol is purely for writing code in a document; it has no meaning to any Smalltalk compiler or IDE.

2) The return symbol in Smalltalk is ^

   MyClass>>myMethod
      ^someCode

means the method myMethod returns the result of someCode.

3) Code evaluation is left to right subject to method precedence. The precedence of a Smalltalk method is:

Thus in the (rather pointless :-)) expression

   12 > 3 ifTrue: [Transcript show: 12 + 9 sqrt].

the binary method > is sent to the SmallInteger 12 with parameter another SmallInteger (3) and only then is the keyword method ifTrue: sent to the result (which is the Boolean true), with parameter a BlockClosure. When the block is evaluated, the unary method sqrt is sent first to 9 before the binary method + is sent to 12 with parameter the result (3). Lastly, the keyword method show: is sent to the Transcript with the result of the addition (15) as parameter.

These precedence rules allow Smalltalk to be written with a relatively small number of brackets and let text epressions read reasonably naturally. However a side-effect is that arithmetic precedence is not the usual one. The result of

   1 + 2 * 5

is 15 (1 added to 2 then all multiplied by 5) not 11; to get the latter, you must write

   1 + (2 * 5)

As everyone, not just Java and C programmers, is likely to be more familiar with the idea of multiplication and division having higher precedence than addition and subtraction, this is a cost you pay for the clarity of the syntax in other areas; you just have to remember it. (One of the code-checking utility rules detects all occurences of the pattern A + B * C and similar in code you write, presenting them for your review, so helps you remember.)

Back to new code syntax

Back to polymorphism instead of a case statement

(Written by Niall Ross as part of Smalltalk for Java Programmers.)


Edit Rename Upload Download Back to Top