Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 38 ยท Syntax and Messages

Smalltalk Syntax on a Postcard: Literals, Comments and Keywords

Learn the complete Smalltalk syntax: number, character, string, symbol and array literals, comments, statements, the six reserved words, and how to read a line of Smalltalk code.

Smalltalk's creators joked that the whole syntax fits on a postcard, and it nearly does. There are literal forms for numbers, characters, strings, symbols and arrays; a way to write comments; a handful of punctuation marks; and six reserved words. This lesson tours all of them, so that nothing in later chapters looks unfamiliar.

The Smalltalk code on this page is read-only. Runnable JavaScript stand-ins illustrate the ideas.

Comments and statements

A comment is text in double quotes. Strings use single quotes, so the two never get confused:

SMALLTALK
"This is a comment. It can span
several lines."
'This is a string'

A statement ends with a period. The last statement in a block or method does not need one:

SMALLTALK
| x |
x := 3.
x := x + 1.
x printString

Number literals

SMALLTALK
42            "integer"
-17           "negative integer"
1000000000000 "grows as large as you need"
3.14          "float"
1.5e3         "float with exponent: 1500.0"
2r1010        "radix: binary 1010 = 10"
16rFF         "hexadecimal FF = 255"
36rZZ         "radix 36"
1/3           "not a literal: the message / sent to 1 gives a Fraction"
1_000_000     "underscores allowed as digit separators in Pharo"

The radix form base r digits works for bases 2 to 36. Fractions and complex expressions are built by sending messages, not literal syntax.

Characters, strings and symbols

SMALLTALK
$a            "the Character a"
$   "a space" "write $ followed by a space for a space character"
'hello'       "a String"
'it''s'       "double the quote to include one: it's"
#hello        "a Symbol: unique, compared by identity"
#with:with:   "symbols can hold keyword selectors"
#+            "binary selector as a symbol"

A Symbol is a unique string: two symbols spelled the same are the same object, so #abc == #abc is true. Symbols are used as selectors, dictionary keys and names. There are no escape sequences inside strings: a newline is built with String cr or String lf, or with String new: 0 and streams, and Character tab gives a tab.

Arrays

SMALLTALK
#(1 2 3)                 "literal array of numbers"
#(1 $a 'two' #three)     "elements can be mixed"
#(foo bar)               "inside #( ) bare words are symbols: #(#foo #bar)"
#(true false nil)        "these are the actual objects true, false, nil"
#[1 2 255]               "byte array literal"
{ 1 + 1. 'a' size. 3 }   "brace array: each element is an expression"

Literal arrays are fixed at compile time and all their contents are literals. Use braces when elements need to be computed. One word of caution: modifying a literal array inside a method changes the method's own constant, so copy it first with copy if you plan to change it.

Variables and assignment

SMALLTALK
| count total |      "declare temporaries"
count := 0.           "assignment: := (older code wrote โ†)"
total := count + 10.

Variable names begin with a letter or underscore. By convention:

Kind Starts with Example
Local, instance variables, arguments lowercase customerName
Classes, globals, class variables, pools Uppercase OrderedCollection, Transcript

Arguments of methods and blocks cannot be assigned to.

The six pseudo-variables and the return arrow

Word Meaning
self The receiver of the current message
super Same object, but method lookup begins in the superclass
thisContext The current execution frame (used by the debugger)
nil The undefined object
true, false The two Boolean objects

^ returns a value from a method: ^ total. Without it, a method returns self. We treat all of them again in Variables, scope and pseudo-variables.

Blocks

Square brackets create a block, a piece of code that is run later:

SMALLTALK
[ 3 + 4 ]                      "no parameters"
[ :x | x * x ]                 "one parameter"
[ :a :b | a max: b ]           "two parameters"
[ :n | | tmp | tmp := n + 1. tmp * 2 ]   "temporary inside a block"

Parameters are written with a leading colon and separated from the body by a vertical bar. See Blocks and closures.

Reading a line like a Smalltalker

SMALLTALK
Transcript show: (3 + 4) printString; cr.

Read from the left: the receiver Transcript gets the keyword message show: with the argument (3 + 4) printString, which is the unary message printString sent to the result of 3 + 4. The semicolon sends cr to Transcript as well. The parentheses are essential, and the rules of precedence come next.

JS Stand-in: tokenizing Smalltalk literals

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: symbols are unique, strings are not

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. How do you include a single quote inside a Smalltalk string?

    Strings are delimited by single quotes and a quote inside is doubled. Double quotes are for comments.
  2. 2. What does #(foo bar) contain?

    Inside a literal array, bare words are read as symbols.
  3. 3. Which literal is created with braces and evaluates each element?

    A brace array evaluates each expression, separated by periods, and builds an Array at run time.
  4. 4. What does 16rFF mean?

    The prefix 16r means radix 16. 2r1010 is binary for 10.
  5. 5. What does a method return if it has no ^ statement?

    Without an explicit return a method answers its receiver, self.

Summary

  • Comments use double quotes; strings use single quotes (doubled to include one).
  • Numbers support radix (16rFF), exponents and automatic large integers; fractions come from /.
  • #sym is a unique Symbol; #(...) is a literal array; { ... } is a computed array.
  • Temporaries go between | |; assignment is :=; ^ returns.
  • Blocks are [ :x | ... ]; the pseudo-variables are self, super, thisContext, nil, true, false.

Next: Unary, Binary and Keyword Messages