Previous
Contents
Next

Squeak - Language Elements


Smalltalk is based on very few elementary language elements and a small number of general rules. The many ready-to-use software components that you find in a Smalltalk image are all defined in Smalltalk itself.

The elementary languages elements are:

Numbers

Integer numbers:

543234544563
-8767888876

Integers to an arbitrary base

The base is always written in decimal notation. The highest possible value for the base is 37.

5r421

Hexadecimal integers are written as integers to the explicit base 16. Capital letters must be used for the digits A to F:

16rF043
16r7DFF

Fractions

A fraction is is written with an integer numerator and an integer denominator:

7/12

This is in fact not a denotation, but an expression that sends the message / to the integer number 7. Upon evaluation, the message / creates a new instance of Fraction.

Different bases can be used to write the numerator and the denominator:

8r55/16rA0

Float numbers

43.5
2.54e-7
-5.4e12

Strings

A string is a sequence of characters enclosed in apostrophes. An apostrophe within a string has to be written twice.

'Squeak'
'Squeak isn''t a bad language'

Character Constants

$A    $a    $$

Note that 'A' is not the same as $A. You see this when you examine the class of both literals:

'A' class
=> String
$A class
=> Character

'A' is a string of size 1 but $A is a character.

Variables

Different writing conventions have to be observed for different types of variables. Instance variables, method parameters, temporary variables all begin with a small letter:

 counter
 numberOfElements
 x12a

Class variable names and the names of global variables begin with a capital letter:

TextConstants
Display
Sensor

Comments

A comment is a text that is written between double quotes. The comment itself may not contain the double quote.

" This is a comment "

Symbols

Examples of symbols:

#name
#at:put:
#==
#'us-ascii'

Symbols are used as method selectors.

Array Denotations

The denotation of an array begins with the hash character #, which is followed by a left parenthesis. Next follows an enumeration of the array elements. A right parenthesis is used to complete the denotation. Aray elements are separated by whitespace characters, no other separators are used.

#(1 4 9 16)

This is an array with four elements. All four elements are integer numbers.

  | a |
a := #(1 4 9 16).
a size
 4
  | a |
a := #(1 4 9 16).
a allSatisfy: [:element | element isKindOf: Integer]
 true

A two-dimensional array is written as an array of arrays:

#(#(1 2 3) #(4 5 6))

To access the first element of the second subarray, one has to write:

  | a |
a := #(#(1 2 3) #(4 5 6)).
(a at: 2) at: 1
 4

Note that it is not possible to write:

a at: 2 at: 1

The elements of an array are not required to be of uniform type. It is completely possible to create an array that contains both numers and strings:

#(1 'Hello' 2.6 -3.14e2)

Statements

Messages

Messages represent the interactions between the objects of a program. A message requests an operation on the part of the receiver. The receiver performs the requested operation and answers a result.

Three kinds of messages are available:

Unary messages have precedence before binary and keyword messages. Binary messages have precedence before keyword messages.

Cascading

There is one special syntactic form called cascading that specifies multiple messages to the same object. This special form is not really needed; it is always possible to rewrite a cascaded message in a form that does not use cascading. However, cascading often reduces the need for using variables.

Assignments

An assignment updates the value of a variable with the result of a computation:

  x := 3*3 + (4*4).
  directions := #('north' 'east' 'south' 'west').

More than one assignment prefix can be used, indicating that the values of several variables are changed:

  x := x0 := 0. 

Blocks

Blocks are objects used in many of the control structures in Smalltalk. A block represents a deferred sequence of actions. A block expression consists of a sequence of expressions separated by periods and delimited by square brackets:

  [index := index + 1]

When a block expression is encountered, the statements enclosed in the brackets are not immediately executed. The value of a block expression is an object that can execute these enclosed statements at a later time, when requested to do so.

The sequence of actions a block describes will take place when the block receives the unary message value. For example, the following two expressions have identical effects:

  index := index + 1
  [index := index + 1] value

A block that contains no expressions returns nil when sent the message value. The expression [ ] value has the value nil.

Block arguments

A block may take one or more arguments. Block arguments are specified by including identifiers preceded by colons at the beginning of a block. The block arguments are separated from the expressions that make up the block by a vertical bar. The following two examples show blocks with one argument:

  [:array | total := total + array size]
  [:item | item * item]

A block with one argument responds to the message value: by setting the block argument to the argument of the value: message and then executing the expressions in the block.

Blocks can take more than one argument:

 [ :x :y | (x * x) + (y * y)]

A block must have the same number of block arguments as the number of value: keywords in the message to evaluate it. If a block receives an evaluation message with a different number of arguments from the number of arguments it takes, an error will be reported.


Previous
Contents
Next