Smalltalk
CSCI 334: Principles of Programming Languages
Williams College�Spring 2026
Announcements
Learning Objectives
Example: Expression Hierarchy
e = new Sum(new Number(23), new Number(2));
print e.toString() + " = " + e.evaluate();
+
23
2
abstract class Expr {
public abstract String toString();
public abstract int eval();
}
class Number extends Expr {
private int n;
public Number(int n) { this.n = n; }
public String toString() { return "" + n; }
public int eval() { return n; }
}
class Sum extends Expr {
private Expr left, right;
public Sum(...) { ... }
public String toString() {
return left.toString() + "+" + right.toString();
}
public int eval() { return left.eval() + right.eval(); }
}
General Concept�(abstract class or interface)
Operations on all Exprs
Implementations for Number
Implementations for Sum
Steve Jobs on Touring Xerox PARC
And they showed me really three things. But I was so blinded �by the first one I didn't even really see the other two. One of �the things they showed me was object orienting programming - they showed me that but I didn't even see that. The other one they showed me was a networked computer system...they had over a hundred Alto computers all networked using email etc., etc., I didn't even see that. I was so blinded by the first thing they showed me which was the graphical user interface... within you know ten minutes it was obvious to me that all computers would work like this someday.
Dynabook
Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I
Design Principles Behind Smalltalk
John McCarthy, 1960��A programming system called LISP (for LISt Processor) has been developed for the IBM 704 computer by the Artificial Intelligence group at M.I.T. ... In this article, we first describe a formalism for defining functions recursively.
Dan Ingalls, 1981��The purpose of the Smalltalk project is to provide computer support for the creative spirit in everyone
Smalltalk: Try It!
Example: Point Class
Class definition written in tabular form
class vars
pi
super class
Object
class name
Point
instance vars
x y
class messages and methods
〈…names and code for methods...〉
instance messages and methods
〈…names and code for methods...〉
Constructors
Instance Messages and Methods
Instance methods
moveDx: dx Dy: dy | |
x <- dx + x
y <- dy + y
In Java:
void moveDxDy(int dx,
int dy) {
x = x + dx;
y = y + dy;
}
Usage
pt moveDx: 1 Dy: 1
pt.moveDxDy(1,1);
Instance Messages and Methods
Instance methods
moveDx: dx Dy: dy | |
x <- dx + x
y <- dy + y
x: xcoord y: ycoord | |
x <- xcoord
y <- ycoord
void xy(int xcoord,
int ycoord) {
x = xcoord;
y = ycoord;
}
Usage
pt moveDx: 1 Dy: 1
pt x:3 y:2
pt.xy(3,2);
Instance Messages and Methods
Instance methods
moveDx: dx Dy: dy | |
x <- dx + x
y <- dy + y
x: xcoord y: ycoord | |
x <- xcoord
y <- ycoord
x | | ^x
y | | ^y
draw | |
〈...draw point...〉
Examples
pt moveDx: 1 Dy: 1
pt x:3 y:2
z <- pt x + pt y
Class Messages and Methods
Class methods
newX: xval Y: yval | |
^ self new x: xval y: yval
(new is method inherited from Object)
class Point {
static Point newXY(int xval,
int yval) {
Point temp = new Point();
temp.xy(xval, yval);
return temp;
}
}
Examples
p <- Point newX:2 Y:3
p = Point.newXY(2,3);
Class Meta Data
x
y
newX:Y:
draw
moveDx:Dy:
Point class
Template
Method dictionary
...
...
(super class)
Run-time Representation
x
y
newX:Y:
draw
moveDx:Dy:
Point class
Template
Method dictionary
...
...
(super class)
2
3
Point object
Inheritance
class var
super class
Point
class name
ColorPoint
instance var
color
class messages and methods
instance messages and methods
newX:xv Y:yv C:cv
〈 … code … 〉
draw
〈 … code … 〉
color
| | ^color
New instance Variable
New method
Override method
ColorPoint Methods
Instance Methods
x: xcoord y: ycoord c:col | |
x <- xcoord
y <- ycoord
color <- c
color | | ^color
draw | | ...
Class Methods
newX: xv Y: yv C:cv ||
^self new x:xv y:yv c:cv
newOrigin ||
^self newX:0 Y:0 C:red
Run-time Representation
x
y
newX:Y:
draw
moveDx:Dy:
Point class
Template
Method dictionary
...
...
(super class)
2
3
Point object
4
5
x
y
newX:Y:C:
color
draw
ColorPoint object
ColorPoint class
Template
Method dictionary
red
color
...
Collection Hierarchy
Collection
Indexed
Set
SortedCollection
Array
Dictionary
Inheritance
Subtyping
Updatable
isEmpty, size, includes: , …
add:
remove:
at:Put:
at:
contains:
replaceFrom:to:With:
sort
Ingalls Test for OO Languages
In an OO language, you should be able to:
Number
Float
Integer
BigInteger