Instructors' Guide


Introduction Terminology Expressions Control Objects Inheritance Technology Summary

Expressions

The syntax of Smalltalk needs some time to get used to. Since everything is an object, expressions may be regarded as being composed of constants, variables and method expressions.

There is a large variety of literal constants (including numbers, characters, strings, symbols, byte arrays and literal arrays), as depicted in slide sm-expr-1.


Literal constants


slide: Smalltalk -- expressions (1)

Expressions may be assigned to variables. Usually, variables are given a name that betrays their expected type, as for example anInteger. (In Smalltalk, class names start with an upper case and variables with a lower case letter.)

Assignment

Variables

Block expressions


slide: Smalltalk -- expressions (2)

We distinguish between temporary variables (having a method scope), instance variables (having an object scope), class variables (having as their scope the collection of instances of the class), pool variables (that have a category as their scope) and global variables (that are visible everywhere). See slide sm-expr-2.

A special kind of expression is the block expression that consists of a program fragment, possibly parametrized with an argument. Block expressions are used to define control structures employing message expressions. Block expressions correspond to function literals (lambda-expressions) in languages such as Lisp and Smalltalk.

Message expressions may be characterized as either unary, binary or keyword messages. See slide sm-expr-3.


Message expressions

  • unary, binary, keyword

Unary

  • 1.0 sin , Random new

Binary

  • arithmetic -- ctr + 1
  • comparison -- aVar >= 200
  • combination -- 100 @ 200
  • association -- # Two -> 2

slide: Smalltalk -- expressions (3)

Unary messages consist of a single method name addressed at an expression denoting an object, for example a constant or a class.

As binary method expressions, we have the familiar arithmetic and comparison expressions as well as the less familiar combination expression (used for graphics coordinates) and association expression (used to define associative maps). All binary (infix) message selectors have the same precedence and bind to the left. Despite their common appearance, these are all true message expressions (which may lead to surprises, for example in the case of a non-commutative definition of the arithmetic operations). Examples of keyword message selectors are given in slide sm-control.