Smalltalk was designed as the language for the Dynabook:
Smalltalk is object-oriented, taking important ideas from Simula:
Scribe penup. Scribe go: 500
The dominant paradigm of programming in Smalltalk is simulation: internally represented objects model the properties and behavior of real-world objects.
Classes -- objects are instances of some class or type of object
an abstraction that defines the properties or behaviors common to all objects of that type
instances of a class may differ in state
Inheritance -- classes are organized in an hierarchy of subclasses and superclasses, and instance variables and methods of a superclass are inherited by all subclasses
a class method may redefine a superclass method
if a method or variable is not found locally, the superclass is searched (recursively) for its definition
hierarchical classification precludes orthogonal classification (see Figures 12.8 - 12.11)
everything in Smalltalk is an object classes are objects
every object is an instance of a class classes are instances of the class Class
Abstract Data Types -- a class is a particular representation of some ADT
predefined classes correspond to data structures in procedural languages
Integer Character Array String Set LinkedList Stream Dictionary
classes for graphics
Point Line Rectangle BitMap Pen
classes for the user interface and file handling
Directory DiskBrowser CursorManager Prompter
classes for controlling the system
Dispatcher Debugger Compiler Context
Defining New Classes -- problems are solved in Smalltalk by defining new classes
a class is defined by specifying
a method consists of
names are not typed -- any object may be bound to any name
dynamic type checking -- if an object responds to a message, it's legal, otherwise, it's not
Three types of message:
unary, e.g., aList size
arithmetic, e.g., aList size + 5
keyword, e.g., aList size + 5 between: 0 and: q top
Precedence is in the order shown above, from left to right within each type
4 factorial between: 3 + 4 and: 'hello' size * 7
24 between: 3 + 4 and: 5 * 7
24 between: 7 and: 35
Note that strings are enclosed in single quotation marks.
Control consists of sending a message to an object (which may send messages to other objects).
Iteration is accomplished using a class called Block (or Context in Smalltalk/V)
An instance of Block is an object which executes some expressions
when it is sent the message value.
creates an instance of a Block which can send the message Index Index + 1 when it receives the message value.
creates an instance of a Block and sends it the message value; in response, the block executes the expression Index Index + 1 which sends the message + with argument 1 to the object Index and then binds the name Index to the new object returned.
Assignment can be used to bind a name to a block.
The block may then be evaluated by
Blocks can be used for definite iteration by sending the message timesRepeat:
to an integer with the block as the argument.
The integer responds to timesRepeat: by sending the message value to the block argument as many times as its own value.
Blocks can take arguments.
The object bound to Total is now the integer 7.
Note that character literals are specified by a $ prefix, and that array literals are specified by a # prefix to a list in parentheses. The symbol @ is a binary operator that creates an object of class Point. Arrays need not be homogeneous.
The message do: takes a block as an argument and can be sent to a Collection object (e.g., an Array)
The Collection responds by sending a value: message to the block for each element of the collection.
The block argument is bound to each element in turn as the value: messages
are sent (i.e., each element is an argument to one value: message).
Five value: messages are sent to the block with the objects 2, 3, 5, 7, and 11 as arguments in turn. In the end, sum is bound to the object 208.
Blocks can be conditionally executed using the classes True and False (subclasses of Boolean).
The messages ifTrue: and ifFalse: (and their combinations) can be sent to objects of these classes, with blocks as arguments.
True responds by sending the message value to the argument block of ifTrue:
False responds by sending value to the argument of ifFalse:
number responds to the arithmetic message \\ by returning its modulus with respect to the argument 2.
The message = with argument 0 results in the return of an object of class True or False.
The ifTrue:ifFalse: message to this object results in the message value being sent to exactly one of the two blocks, which results in the binding of parity to one of the objects 0 or 1.
Compound Boolean expressions may be constructed using and: and or:
The receiver must be an object of class Boolean.
The argument must be a block which returns an object of class
Boolean when sent the message value.
and: and or: use lazy evaluation.
Conditional repetition is implemented by sending the message whileTrue: (or whileFalse:)
to a block with another block as argument.
The receiver block sends itself the message value and if the response is an object from class True then it sends the message value to the argument block and starts over again.
If the result of sending itself value is an object from the class False, the object nil is returned.
Recursion is possible, since an object can send messages to itself.