1 of 22

Smalltalk

CSCI 334: Principles of Programming Languages

Williams College�Spring 2026

2 of 22

  • HW 6, Lab 6�
  • Reading Growing a Language for next Tuesday�
  • Midterm will be returned this afternoon
    • mean: ~48/60 (~80%)
    • challenging exam, time a factor for some
    • everything is scaled at end of semester
    • let's chat if you're concerned about where you stand

Announcements

3 of 22

  • Review OO Design and Language Features
  • Understand the design and implementation of Smalltalk

Learning Objectives

4 of 22

Example: Expression Hierarchy

  • Define general concept: Expression
  • Implement two forms: Number, Sum
  • Methods on implemented types of exprs
    • evaluate, toString, draw, ...
  • Ex:

e = new Sum(new Number(23), new Number(2));

print e.toString() + " = " + e.evaluate();

  • Anticipate additions to library

+

23

2

5 of 22

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

6 of 22

7 of 22

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.

8 of 22

Dynabook

  • "A Personal Computer for Children of All Ages", Alan Kay, 1972

9 of 22

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

10 of 22

Smalltalk: Try It!

  • http://squeak.org/

11 of 22

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

12 of 22

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

13 of 22

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

14 of 22

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

15 of 22

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

16 of 22

Class Meta Data

x

y

newX:Y:

draw

moveDx:Dy:

Point class

Template

Method dictionary

...

...

(super class)

17 of 22

Run-time Representation

  • Three primary operations
    • object creation
    • method lookup
    • field lookup

x

y

newX:Y:

draw

moveDx:Dy:

Point class

Template

Method dictionary

...

...

(super class)

2

3

Point object

18 of 22

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

19 of 22

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

20 of 22

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

...

21 of 22

Collection Hierarchy

Collection

Indexed

Set

SortedCollection

Array

Dictionary

Inheritance

Subtyping

Updatable

isEmpty, size, includes: , …

add:

remove:

at:Put:

at:

contains:

replaceFrom:to:With:

sort

22 of 22

Ingalls Test for OO Languages

In an OO language, you should be able to:

  • Define a new kind of integer,
  • Put your new integers into a rectangle,
  • Ask the system to fill in the rectangle, and
  • Have it work.

Number

Float

Integer

BigInteger