topical media & game development

object-oriented programming

# The language Smalltalk

Smalltalk has been, without doubt, the most influential of all object-oriented programming languages. Originally meant as an easy-to-use programming language for the Dynabook (a laptop avant-la-lettre developed in 1972 at Xerox Parc), it has developed into a powerful general purpose programming language (which has stabilized in Smalltalk-80) that runs on many platforms. From the start, an interactive programming environment has been an integral part of the language implementation. Later implementations also include support for the interactive construction of user interfaces.

#### Smalltalk -- a radical change in programming

A


1972  Dynabook -- Alan Kay
1976  SmallTalk76
1980  SmallTalk80
1990  ObjectWorks/SmallTalk -- VisualWorks


#### Design principles -- rapid prototyping

• uniform object model -- control
• dynamic typing -- flexible
• standard libraries -- functionality

slide: The language Smalltalk

Influenced by the ideas of objects and classes embodied in Simula, the design philosophy underlying Smalltalk clearly reflects the desire to effect nothing less than a radical change in programming practice. Characteristic for the design of Smalltalk is a uniform object model (which is even used to support common control constructs), dynamic typing (which accounts for much of the flexibility of Smalltalk) and a sizeable collection of standard library classes (providing the functionality necessary to build complex applications). Smalltalk has successfully been used, in particular, for rapid prototyping.

#### Terminology

The introduction of Smalltalk came along with an, at the time, astounding terminology. See slide sm-terminology.

#### Terminology

Smalltalk

• literal -- constants
• object -- action by messages
• class -- collection of protocols
• protocol -- related methods
• category -- collection of classes

slide: Smalltalk -- terminology

Most important is the notion of object, which is something that acts in response to messages (by executing a method procedure). In Smalltalk, everything is an object. Moreover, every object is an instance of a class. A class is the description of a collection of objects which share the same structure and applicable methods. The methods of both objects and classes (considered as an object) are grouped in so-called protocols. Related collections of classes may be grouped in so-called categories. Both protocols and categories are merely syntactic add-ons, meant to facilitate programming.

#### 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

• number -- 1, 34.6, 8r24
• character -- $a,$b, ...
• string -- "this is a string"
• symbol -- #Float
• byte array -- # [0 255 2 7]
• array of literals -- # (12.1 # ($a$b))

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.)

• aVar := 1.

#### Variables

• temporary, instance, class, pool, global

#### Block expressions

• [ :arg | expression ]

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.

#### Control

Smalltalk has no control structures except message passing. However, familiar control structures are defined as methods on booleans and integers. See slide sm-control.

#### Keyword methods


(i <= 7)
ifTrue: [ m:= "oke" ]
ifFalse: [ ... ]


#### Control structures

• conditional -- ifTrue: ifFalse:
• iteration -- whileTrue:
• looping -- to: by: do: [ :i | ... ]

slide: Smalltalk -- control

For example, an if-statement may be obtained by defining the method ifTrue: ifFalse: on booleans. (Despite the use of keywords, parameter passing in Smalltalk is positional. Each sequence of keywords may be regarded as a different method.) In a similar vein, we may define iteration and looping. For looping, we may employ the parameter mechanism of blocks, as indicated above.

#### Objects

Everything in Smalltalk is an object. An object may be regarded as consisting of instance variables and a collection of methods.

#### Object -- behavior

• instance variables, methods

#### Class -- description

• class variables, class methods
• #### Self -- self reference

• super for ancestors

slide: Smalltalk -- objects (1)

A class is the description of a collection of objects, its instances. Considered as an object, a class may be said to have class variables and class methods.

For self-reference the special expression self may be used. To invoke methods from the parent class the expression super may be used.

An example of an object class description is given in slide sm-objects-2. The class Ctr is defined as a subclass of the class behavior. It supports an initialization protocol (containing the method initialize), a protocol for modification (containing the method add), and an inspection protocol (containing the method value).

#### Example -- class


Behavior subclass: #Ctr
instanceVariableNames: 'value'
Ctr methodsFor: 'initialization'
initialize
value := 0.
Ctr methodsFor: 'modifications'
value := value + aValue.
Ctr methodsFor: 'inspection'
value
^value


slide: Smalltalk -- objects (2)

Note that value occurs both as an instance variable and as a method. Only the method is accessible by the user.

In addition, we need a class description defining the object functionality of Ctr, which consists of an {\em instance creation} protocol defining the class method new. See slide sm-objects-3. This class description is (implicitly) an instance of a meta class generated by the Smalltalk system. See section meta.

#### Class description -- meta class


Ctr class
instanceVariableNames: ''
Ctr class methodsFor: 'instance creation'
new
^super new initialize


slide: Smalltalk -- objects (3)

#### Inheritance

Each class in the Smalltalk library is (ultimately) a subclass of the class Object. See slide sm-inheritance.

#### Inheritance


Object
Magnitude
ArithmeticValue
Number
Integer


slide: Smalltalk -- inheritance

Smalltalk supports only single inheritance. Above, the ancestor classes of the class Integer are depicted as a branch of the inheritance tree.

#### Technology

Inheritance, in combination with message passing, allows for powerful programming techniques. As an example, an illustration is given of the cooperation between two objects employing the Model/View paradigm. The model class Ctr, depicted in slide sm-tech-1, may be regarded as embodying the proper functionality of the application.


Model subclass: #Ctr    Model
...
initialize
value := 0.
TV open: self.
...
value := value + anInt.
self changed: #value.


slide: Smalltalk -- technology (1)


View subclass: #TV    View
instanceVariableNames: ''
TV methodsFor: 'updating'
update: aValue
Transcript show: 'ok'; cr .
TV class
instanceVariableNames: ''
TV class methodsFor: 'instance creation'
open: aCtr
self new model: aCtr


slide: Smalltalk -- technology (2)

A view class defines an object that may be used to monitor the behavior of the model instance in a non-intrusive way. To support monitoring, the model class Ctr needs to install one or more view objects during initialization, and further, it must notify its view object(s) whenever its contents have been modified, as in add. See slide sm-tech-2.

The view class TV defines a class method open to create a new view object for an instance of the Ctr class. It must further define a method update (that will automatically be invoked when the Ctr instance signals a change) to display some message, for example the value of the Ctr object monitored.

The programming environment forms an integral part of the Smalltalk system. The code depicted above (which clearly reflects the object nature of classes) is usually not the result of text editing, but is generated by the system. The Smalltalk programming system, in particular the standard library, however, will take some time to get familiar with.

#### Summary

This section presented a brief introduction to the programming language Smalltalk.

#### The language Smalltalk

A

• design principles -- prototyping
• terminology -- object, class, protocols
• syntax -- method expressions
• objects -- self reference
• inheritance -- class hierarchy