Scheme object-oriented programming with closures
last modified November 25, 2026 Guile 3.0
An object bundles state with the operations on that state. In Scheme, a closure already does this: it keeps the variables of the environment in which it was created. If the closure takes a symbol naming an operation, a message, and chooses what to do with it, the closure behaves like an object. This style is called message passing. This article builds objects, inheritance, and polymorphism from closures alone; the GOOPS library of Guile provides the same features as a full object system. The examples are run with GNU Guile 3.0.7.
Objects as procedures
A constructor is a procedure that returns a closure. The closure takes a
message and dispatches on it with case, from the
conditionals article.
(define (make-point x y)
(lambda (message)
(case message
((x) x)
((y) y)
((norm) (sqrt (+ (* x x) (* y y))))
(else 'unknown-message))))
(define p (make-point 3 4))
(define q (make-point -1 2))
(display (p 'x))
(display " ")
(display (p 'y))
(display " ")
(display (p 'norm))
(newline)
(display (q 'x))
(display " ")
(display (q 'colour))
(newline)
make-point returns a procedure that remembers
x and y. Calling it with the symbol
x returns the x coordinate; norm computes the
distance from the origin. Each call to make-point creates a
separate object with its own coordinates. A message that the object does
not understand returns the symbol unknown-message, which is
easy to miss; the next section reports it as an error.
$ guile point.scm 3 4 5 -1 unknown-message
Messages with arguments
Some messages need arguments, and some change the state of the object. A
rest parameter collects the arguments that follow the message, and
set! changes the variables that the closure holds.
(define (make-counter name)
(let ((count 0))
(lambda (message . args)
(case message
((name) name)
((value) count)
((increment!)
(set! count (+ count (if (null? args) 1 (car args))))
count)
((reset!) (set! count 0) count)
(else (error "unknown message" name message))))))
(define visitors (make-counter "visitors"))
(define errors (make-counter "errors"))
(visitors 'increment!)
(visitors 'increment! 10)
(errors 'increment!)
(display (visitors 'name))
(display ": ")
(display (visitors 'value))
(newline)
(display (errors 'name))
(display ": ")
(display (errors 'value))
(newline)
(visitors 'reset!)
(display (visitors 'value))
(newline)
(visitors 'decrement!)
The closure takes the message and any number of further arguments in the
list args, as described in the
procedures article.
increment! adds its optional argument or 1. The two counters
have separate counts. The state is private: only messages can read or
change count. An unknown message now calls
error, so the mistake stops the program with a clear message
instead of producing a strange value. The backtrace is omitted from the
output.
$ guile counter.scm visitors: 11 errors: 1 0 counter.scm:11:14: unknown message "visitors" decrement!
Self references
A method often needs another method of the same object. When the
dispatcher is defined with an internal define, it has a name,
and the methods can send messages to it.
(use-modules (ice-9 format))
(define (make-rectangle width height)
(define (self message . args)
(case message
((width) width)
((height) height)
((area) (* width height))
((perimeter) (* 2 (+ width height)))
((square?) (= width height))
((scale!)
(set! width (* width (car args)))
(set! height (* height (car args))))
((describe)
(format #f "~ax~a rectangle, area ~a, perimeter ~a~a"
(self 'width) (self 'height) (self 'area) (self 'perimeter)
(if (self 'square?) ", a square" "")))
(else (error "rectangle: unknown message" message))))
self)
(define r (make-rectangle 3 4))
(display (r 'describe))
(newline)
(r 'scale! 2)
(display (r 'describe))
(newline)
(define s (make-rectangle 5 5))
(display (s 'describe))
(newline)
(define (send object message . args)
(apply object message args))
(send s 'scale! 3)
(display (send s 'area))
(newline)
(display (map (lambda (shape) (send shape 'perimeter)) (list r s)))
(newline)
The dispatcher is called self, and the constructor returns
it. describe sends width, area, and
other messages to self instead of computing them again, and
format from the ports article
builds the text. After scale!, the description reflects the
new size. send is a small helper that makes the message
syntax explicit; it uses apply to pass a list of arguments.
Objects are ordinary procedures, so map works over a list of
them.
$ guile self.scm 3x4 rectangle, area 12, perimeter 14 6x8 rectangle, area 48, perimeter 28 5x5 rectangle, area 25, perimeter 20, a square 225 (28 60)
Inheritance by delegation
Inheritance lets a new kind of object reuse the behaviour of an existing one. With closures, an object creates a parent object and delegates to it every message that it does not handle itself.
(define pi (* 4 (atan 1)))
(define (make-shape name colour)
(lambda (message . args)
(case message
((name) name)
((colour) colour)
((paint!) (set! colour (car args)))
((describe) (string-append colour " " name))
(else (error "unknown message" name message)))))
(define (make-circle radius colour)
(let ((parent (make-shape "circle" colour)))
(lambda (message . args)
(case message
((radius) radius)
((area) (* pi radius radius))
(else (apply parent message args))))))
(define (make-square side colour)
(let ((parent (make-shape "square" colour)))
(lambda (message . args)
(case message
((side) side)
((area) (* side side))
(else (apply parent message args))))))
(define c (make-circle 2 "red"))
(define s (make-square 3 "blue"))
(display (c 'describe))
(display ", area ")
(display (c 'area))
(newline)
(c 'paint! "green")
(display (c 'describe))
(newline)
(display (s 'describe))
(display ", area ")
(display (s 'area))
(newline)
(display (list (c 'radius) (s 'side)))
(newline)
make-shape handles what all shapes share: a name and a
colour. A circle and a square each create a shape as their parent and
handle their own messages, such as area. Every other message
goes to the parent with apply, so describe and
paint! work for both. The value of pi is
computed as 4 arctan 1. The shape's describe cannot mention
the area, because the shape does not know the object that delegates to
it.
$ guile delegation.scm red circle, area 12.566370614359172 green circle blue square, area 9 (2 3)
Overriding and calling the parent
To let a parent method use the methods of the child, the child passes
itself to the parent as self. The parent then sends messages
to the whole object, and a child can override a parent method
by handling the message itself.
(use-modules (ice-9 format))
(define pi (* 4 (atan 1)))
(define (make-shape self name)
(lambda (message . args)
(case message
((name) name)
((describe)
(format #f "~a with area ~,2f" (self 'name) (self 'area)))
((bigger-than?) (> (self 'area) ((car args) 'area)))
(else (error "unknown message" name message)))))
(define (make-circle radius)
(define (self message . args)
(case message
((area) (* pi radius radius))
(else (apply parent message args))))
(define parent (make-shape self "circle"))
self)
(define (make-rectangle width height)
(define (self message . args)
(case message
((area) (* width height))
(else (apply parent message args))))
(define parent (make-shape self "rectangle"))
self)
(define (make-square side)
(define (self message . args)
(case message
((area) (* side side))
((describe)
(string-append (parent 'describe) (format #f ", side ~a" side)))
(else (apply parent message args))))
(define parent (make-shape self "square"))
self)
(define c (make-circle 1))
(define r (make-rectangle 2 3))
(define s (make-square 2))
(display (c 'describe))
(newline)
(display (r 'describe))
(newline)
(display (s 'describe))
(newline)
(display (list (r 'bigger-than? c) (c 'bigger-than? s)))
(newline)
make-shape now takes self and calls
(self 'area); the area comes from the child. Each child
defines its dispatcher first and its parent second, because the parent
needs the dispatcher as an argument. Internal definitions are evaluated in
order, and creating the parent only stores self without
calling it. The square overrides describe: it calls the
parent's version through parent, like a call to a superclass
method in other languages, and adds the side. bigger-than?
takes another shape as its argument and sends it a message.
$ guile override.scm circle with area 3.14 rectangle with area 6.00 square with area 4.00, side 2 (#t #f)
Polymorphism
Objects of different kinds that understand the same messages can be used interchangeably. This is polymorphism. The shapes move into a module, as described in the modules article.
(define-module (shapes)
#:use-module (ice-9 format)
#:export (make-circle make-rectangle make-square))
(define pi (* 4 (atan 1)))
(define (make-shape self name)
(lambda (message . args)
(case message
((name) name)
((describe)
(format #f "~a with area ~,2f" (self 'name) (self 'area)))
((bigger-than?) (> (self 'area) ((car args) 'area)))
(else (error "unknown message" name message)))))
(define (make-circle radius)
(define (self message . args)
(case message
((area) (* pi radius radius))
(else (apply parent message args))))
(define parent (make-shape self "circle"))
self)
(define (make-rectangle width height)
(define (self message . args)
(case message
((area) (* width height))
(else (apply parent message args))))
(define parent (make-shape self "rectangle"))
self)
(define (make-square side)
(define (self message . args)
(case message
((area) (* side side))
((describe)
(string-append (parent 'describe) (format #f ", side ~a" side)))
(else (apply parent message args))))
(define parent (make-shape self "square"))
self)
The module exports only the constructors. The parent shape and its
describe method are reached through the objects.
(use-modules (shapes) (srfi srfi-1))
(define shapes
(list (make-circle 1.5) (make-rectangle 4 2) (make-square 3)
(make-circle 0.5) (make-rectangle 1 9)))
(for-each (lambda (shape)
(display (shape 'describe))
(newline))
shapes)
(define (total-area shapes)
(fold (lambda (shape sum) (+ sum (shape 'area))) 0 shapes))
(display (round (total-area shapes)))
(newline)
(define (largest shapes)
(reduce (lambda (shape best) (if (shape 'bigger-than? best) shape best))
#f shapes))
(display ((largest shapes) 'describe))
(newline)
(define by-area (sort shapes (lambda (a b) (< (a 'area) (b 'area)))))
(display (map (lambda (shape) (shape 'name)) by-area))
(newline)
(display (count (lambda (shape) (string=? (shape 'name) "circle")) shapes))
(newline)
The list holds circles, rectangles, and a square. The program sends the
same messages to each of them, and each object answers in its own way.
fold sums the areas, and reduce finds the
largest shape with bigger-than?. The square comes before the
rectangle of the same area because bigger-than? is strict.
sort from the sorting article
orders the shapes by area, and count counts the circles by
name.
$ guile -L . polymorphism.scm circle with area 7.07 rectangle with area 8.00 square with area 9.00, side 3 circle with area 0.79 rectangle with area 9.00 34.0 square with area 9.00, side 3 (circle circle rectangle square rectangle) 2
Prototypes and method tables
The objects so far have a fixed set of methods written in a
case. Objects can also keep their methods in a
hash table, so that methods can be
added at run time. When an object has no slot of a given name, it asks its
prototype. This style is called prototype-based programming.
(define (make-object prototype)
(let ((slots (make-hash-table)))
(define (lookup name)
(let ((handle (hash-get-handle slots name)))
(cond (handle (cdr handle))
(prototype (prototype 'get name))
(else (error "no such slot" name)))))
(define (self message . args)
(case message
((get) (lookup (car args)))
((set!) (hash-set! slots (car args) (cadr args)))
((own?) (and (hash-get-handle slots (car args)) #t))
(else (apply (lookup message) self args))))
self))
(define animal (make-object #f))
(animal 'set! 'sound "...")
(animal 'set! 'speak
(lambda (self)
(string-append (self 'get 'name) " says " (self 'get 'sound))))
(define dog (make-object animal))
(dog 'set! 'sound "woof")
(define rex (make-object dog))
(rex 'set! 'name "Rex")
(rex 'set! 'speak
(lambda (self) (string-append ((dog 'get 'speak) self) "!")))
(define tom (make-object animal))
(tom 'set! 'name "Tom")
(display (rex 'speak))
(newline)
(display (tom 'speak))
(newline)
(dog 'set! 'fetch
(lambda (self thing) (string-append (self 'get 'name) " fetches the " thing)))
(display (rex 'fetch "ball"))
(newline)
(display (list (rex 'own? 'name) (rex 'own? 'sound) (dog 'own? 'sound)))
(newline)
(tom 'fetch "stick")
make-object creates an object with an empty table of slots
and an optional prototype. get and set! read
and write slots, and own? tells whether the slot belongs to
the object itself. Any other message looks up a method and calls it with
the object as its first argument. Because self is the object
that received the message, speak defined in
animal uses the name of Rex and the sound of the dog. Rex
overrides speak and calls the dog's version through
get. The fetch method is added to the dog after
Rex was created, and Rex can use it at once. Tom is an animal, not a dog,
so fetch is not found.
$ guile prototypes.scm Rex says woof! Tom says ... Rex fetches the ball (#t #f #t) prototypes.scm:7:20: In procedure self: no such slot fetch
Source
Structure and Interpretation of Computer Programs, 2.4 Multiple Representations for Abstract Data
Closure — GNU Guile Reference Manual
In this article we have built objects from closures that dispatch on messages, passed arguments with messages, referred to the object itself, inherited behaviour by delegation, overridden methods and called the parent, used different objects through the same messages, and kept methods in tables of prototype objects. The closures article explains how closures keep their state, and the records article defines data types without behaviour.
Author
List all Scheme tutorials.