Smalltalk Programming/Syntax

Smalltalk has one of the simpler syntaxes of any computer language. There are 3 different types of method call - unary, binary, and n-way keyword. The rules for precedence scan from left to right for each of those 3 flavors of method calling. Precedence can be overridden with parenthesis, as usual.

1. Unary method. Can be as simple as display redrawEverything. ; the object display looks up it's method to do an unqualified redraw, and does it. Unary methods and the object they act on are both alphanumeric words that don't end in a colon. Whenever you see multiple such words in a row, the one on the left is the object, the rest are a gauntlet of method calls.

2. Binary method. In myNumber + 5., the object myNumber is told to look up the method +, and run it with 5 as the argument. Here, the argument is being discarded, the := assignment will come later. Binary method names are 1 or 2 special characters; they only exist for the usual set of suspects in math and logic equations. There is no special precedence for math operators, you must either state them in the order wanted, or use parenthesis.

3. Keyword method. This is where smalltalk diverges from most other programming languages. In display drawFromX:1 fromY:1 toX:50 toY:100., display is being called with one method and 4 arguments. The method's name is drawFromX:fromY:toX:toY:, and the arguments are positionally passed into the method. If you want to call another keyword method without the two blending together, you can use the comma operator, parenthesis, or just carry the result to a new statement.

A statement optionally starts with an assignment, has at least an opening object, and sends at least one method to that object. The statement ends with a period.

A [block] is a bracketed set of statements that are bundled together as a single object, which could be triggered by flow-control style methods. In 4 < 5 ifTrue: [ Transcript print:'4 is less than 5!' ]. , 4<5 evaluates into the boolean object True, who always executes a block passed through the ifTrue: method.

Last modified on 21 July 2009, at 20:30