Previous Table of Contents Next


5.3. The Smalltalk Language

The Smalltalk language is simple—so simple, in fact, that it can be written on one page. The concepts are also simple:

  Everything is an object.
  Objects send messages to each other.
  A message causes the execution of a method.
  A single value (object) is answered as the result of any message send. (This object might be an array of objects or the default value, which is the recipient of the message.)
  Common variables and methods are organized into classes, and similar classes can inherit from a single superclass, which can also inherit.

5.3.1. One-Page Syntax

So few language constructs exist in Smalltalk that the syntax can be written in one page. Although the original one-page syntax of Smalltalk (written one night to win a bet at Xerox PARC) has never been published, the following table provides our one-page summary of the syntax and some of the common constructs.

TABLE 5.1. Smalltalk syntax.

Element Description

“Public - Answer result” A comment
‘String’ A string literal
$X A character literal
[ ] A block
; Cascade messages to same receiver
. End of statement
#symbol A symbol literal
^returnValue Return of a method
:= Assign to variable on left
self Object of method
super Skip self and look to the superclass for the message to follow
true Single instance of class True
false Single instance of class False
nil Single instance of class UndefinedObject
value ifTrue: [] ifFalse: [] Conditional based on value
10 timesRepeat: [] Repeat 10 times
1 to: 10 do: [:i | ] Go through interval of 1-10, passing value into block
[] whileTrue: [] Evaluate second block repeatedly until first block evaluates to false
anObject aMessage A message send with no argument (all message sends return one object)
anObject aMessage: anArgument A message send including an argument (more arguments are added with additional keywords that include a colon and an argument name)
| temporary | A temporary variable at the start of any method

Because Smalltalk is weakly typed (meaning that there is only one type, an object), the declarations of variables cannot make any distinction between types of classes. The message isKindOf: aClass can be sent to any object to determine the class of which it is an instance, and code can proceed from there. The drawback to this type of coding is that it defeats some polymorphism, but it does reduce the number of extraneous methods in commonly used classes such as String and Integer. It is also worth noting that because the developer has total control over Smalltalk, additional rigor can be placed on the declaration of variables, including typing variables by class if necessary.

5.4. Some Simple Smalltalk Code

To better understand the syntax, the following sections show some examples of what Smalltalk code looks like. The examples range from simple to slightly more complex. Any one of the introductory Smalltalk books or online tutorials can provide more detailed examples in the context of a simple application, and more complex examples can be found in Advanced Smalltalk (Pletzke, 1997).

A class in Smalltalk is defined and edited in any of the numerous code browsers or in the debugger. The definition of a simple class, Person, with four instance variables is shown here:

   Object subclass: #Person
           instanceVariableNames: ‘firstName lastName middleInitial
   birthDate’
           classVariableNames: ‘’
           poolDictionaries: ‘’

Notice that the instance variables are not declared with any type or size information. Any variable can contain any value because the type held by each variable is an object, and everything is an object.

One of the simplest types of methods in Smalltalk is an accessor, which provides access to an instance variable for setting the value or getting the value. Most Smalltalk code includes accessors for all variables, and the private or public distinction is made in the method comment. >> is used to denote a method of the class Person, but does not appear in code anywhere. The code is simply entered into a browser opened on the Person class:

   Person>>birthDate
            “Public - Answer the birthDate instance variable”

            ^birthDate


   Person>>birthDate: aDate
            “Public - Set the birthDate instance variable.”

            birthDate := aDate

A more complex method than the accessor is one that actually does something, although accessors can be modified to provide initialization on demand of variables as well as undergo database searches. A simple method that returns a person’s age as of today makes use of the class Person (called self in the method) and the Date class, which understands the message today:

    Person>>age
             “Public - Answer the age of self as of today”

             ^Date today year - self birthDate year

In this method, the message today is sent to the class Date, which answers an instance of Date containing the current day, month, and year. The message year is sent to that instance of Date, which answers an instance of Integer. The - is sent to that Integer after evaluating the argument for the - message, an Integer obtained by sending the message year as the result of the message birthDate sent to self.

Typical Smalltalk coding creates simple methods that are approximately this complex to slightly more complex, and then assembles these methods into comparably simple methods. This results in a lot of methods generated, but the complexity of problems, methods, and solutions is reduced considerably, and similar operations can share common sets of code. Any time a Smalltalk programmer finds that he or she is copying code, he or she can instead create a new method that contains the common code and can refer to it from both (or many) other methods.


Previous Table of Contents Next