Scheme GOOPS
last modified November 25, 2026 Guile 3.0
GOOPS is the object system of GNU Guile. Its name stands for GNU Object-Oriented Programming System. A class describes the slots that its instances hold, and behaviour lives in generic functions: procedures made of several methods, of which the one matching the classes of the arguments is called. The object-oriented programming with closures article builds objects from closures by hand; GOOPS provides classes, inheritance, and method dispatch ready-made. The examples are run with GNU Guile 3.0.7.
A first class
GOOPS lives in the (oop goops) module. define-class
takes the class name, a list of superclasses, and the slot definitions. By
convention, class names are written in angle brackets.
(use-modules (oop goops)) (define-class <point> () (x #:init-value 0) (y #:init-value 0)) (define p (make <point>)) (display (slot-ref p 'x)) (newline) (slot-set! p 'x 3) (slot-set! p 'y 4) (display (list (slot-ref p 'x) (slot-ref p 'y))) (newline) (display (is-a? p <point>)) (newline) (display (class-name (class-of p))) (newline)
The class <point> has no explicit superclass and two
slots, x and y, which start as 0.
make creates an instance. slot-ref reads a slot by
name, and slot-set! changes it. is-a? tests
whether an object is an instance of a class, and class-of
returns the class of any value, also of numbers and strings.
$ guile point.scm 0 (3 4) #t <point>
Init keywords and accessors
Slot options make instances easier to create and to use. An init keyword
sets the slot from an argument of make, and a getter or an
accessor is a procedure that reads the slot.
(use-modules (oop goops)) (define-class <point> () (x #:init-keyword #:x #:init-value 0 #:getter point-x) (y #:init-keyword #:y #:init-value 0 #:getter point-y)) (define-class <account> () (owner #:init-keyword #:owner #:getter owner) (balance #:init-keyword #:balance #:init-value 0 #:accessor balance) (history #:init-thunk (lambda () '()) #:accessor history)) (define p (make <point> #:x 3 #:y 4)) (define q (make <point> #:y 7)) (format #t "p: ~a ~a~%" (point-x p) (point-y p)) (format #t "q: ~a ~a~%" (point-x q) (point-y q)) (define acc (make <account> #:owner "Lucia" #:balance 120)) (set! (balance acc) (+ (balance acc) 30)) (set! (history acc) (cons 30 (history acc))) (format #t "~a: ~a ~a~%" (owner acc) (balance acc) (history acc))
#:init-keyword names the keyword argument of
make; #:init-value is used when the keyword is
missing, so q gets x 0. #:getter defines a
procedure that reads the slot. #:accessor also allows writing:
(set! (balance acc) ...) stores a new balance.
#:init-thunk calls a procedure for each new instance, which
is how a slot gets a fresh value rather than one shared by all instances.
GOOPS ignores keywords that no slot declares, so a misspelled keyword
leaves the slot at its default without an error.
$ guile accessors.scm p: 3 4 q: 0 7 Lucia: 150 (30)
Generic functions and methods
define-method adds a method to a generic function and creates
the generic function if it does not exist yet. Each parameter may be
followed by a class; the method applies only to arguments of these
classes.
(use-modules (oop goops))
(define-class <point> ()
(x #:init-keyword #:x #:init-value 0 #:getter point-x)
(y #:init-keyword #:y #:init-value 0 #:getter point-y))
(define-method (norm (p <point>))
(let ((x (point-x p))
(y (point-y p)))
(sqrt (+ (* x x) (* y y)))))
(define-method (distance (a <point>) (b <point>))
(norm (make <point>
#:x (- (point-x a) (point-x b))
#:y (- (point-y a) (point-y b)))))
(define-method (twice (n <number>)) (* 2 n))
(define-method (twice (s <string>)) (string-append s s))
(define-method (twice (lst <list>)) (append lst lst))
(define p (make <point> #:x 3 #:y 4))
(define q (make <point> #:x 6 #:y 8))
(display (norm p))
(newline)
(display (distance p q))
(newline)
(display (list (twice 21) (twice "ab") (twice '(1 2))))
(newline)
(display twice)
(newline)
(twice 'echo)
norm and distance are methods for points.
distance creates a point for the difference and calls
norm on it. Methods may specialise on built-in classes too:
twice has three methods, for numbers, strings, and lists, and
the class of the argument selects one of them. Displaying the generic
function shows its name and the number of methods. A call that matches no
method is an error. The backtrace is omitted from the output.
$ guile methods.scm 5 5 (42 abab (1 2 1 2)) #<<generic> twice (3)> oop/goops.scm:1585:2: No applicable method for #<<generic> twice (3)> in call (twice echo)
Printing objects
By default an instance prints as its class and an address, which differs
between runs. write and display are generic
functions in GOOPS, so a class can define how its instances are printed.
(use-modules (oop goops))
(define-class <point> ()
(x #:init-keyword #:x #:init-value 0 #:getter point-x)
(y #:init-keyword #:y #:init-value 0 #:getter point-y))
(define-method (write (p <point>) port)
(format port "#<point ~a ~a>" (point-x p) (point-y p)))
(define-method (display (p <point>) port)
(format port "(~a, ~a)" (point-x p) (point-y p)))
(define path (list (make <point>)
(make <point> #:x 2 #:y 1)
(make <point> #:x 5 #:y 3)))
(write (car path))
(newline)
(display (cadr path))
(newline)
(write path)
(newline)
(display path)
(newline)
(format #t "from ~a to ~s~%" (car path) (caddr path))
The write method receives the object and the port. The
display method chooses a friendlier form. Without the
display method, display would use the
write method. The methods apply to points inside lists as
well, and format uses them for ~a and
~s.
$ guile printing.scm #<point 0 0> (2, 1) (#<point 0 0> #<point 2 1> #<point 5 3>) ((0, 0) (2, 1) (5, 3)) from (0, 0) to #<point 5 3>
Inheritance
A class inherits the slots and the methods of its superclasses. A method defined for a superclass applies to the instances of all its subclasses, unless a subclass has a more specific method.
(use-modules (oop goops))
(define-class <shape> ()
(name #:init-keyword #:name #:getter shape-name))
(define-class <circle> (<shape>)
(radius #:init-keyword #:radius #:getter radius))
(define-class <rectangle> (<shape>)
(width #:init-keyword #:width #:getter width)
(height #:init-keyword #:height #:getter height))
(define-class <square> (<rectangle>))
(define-method (area (c <circle>))
(* 3.14159 (radius c) (radius c)))
(define-method (area (r <rectangle>))
(* (width r) (height r)))
(define-method (describe-shape (s <shape>))
(format #f "~a with area ~a" (shape-name s) (area s)))
(define (make-square side)
(make <square> #:name "square" #:width side #:height side))
(define shapes
(list (make <circle> #:name "circle" #:radius 1)
(make <rectangle> #:name "rectangle" #:width 4 #:height 2)
(make-square 3)))
(for-each (lambda (s)
(display (describe-shape s))
(newline))
shapes)
(define sq (caddr shapes))
(display (list (is-a? sq <square>) (is-a? sq <rectangle>)
(is-a? sq <circle>)))
(newline)
(display (map class-name (class-precedence-list <square>)))
(newline)
(display (map class-name (class-direct-subclasses <shape>)))
(newline)
A circle and a rectangle are shapes, and a square is a rectangle without
slots of its own. area has methods for circles and rectangles;
the square uses the rectangle method. describe-shape is
defined once for all shapes and calls area, which dispatches
on the actual class. class-precedence-list shows the order in
which GOOPS searches for methods: from the class itself up to
<object> and <top>, the root of all
classes.
$ guile inheritance.scm circle with area 3.14159 rectangle with area 8 square with area 9 (#t #t #f) (<square> <rectangle> <shape> <object> <top>) (<rectangle> <circle>)
Calling the next method
A method can extend the method of its superclass instead of replacing it.
(next-method) calls the next less specific method with the
same arguments. The initialize generic function, called by
make, is extended in the same way.
(use-modules (oop goops))
(define-class <employee> ()
(name #:init-keyword #:name #:getter name)
(salary #:init-keyword #:salary #:getter salary))
(define-class <manager> (<employee>)
(reports #:init-keyword #:reports #:init-value '() #:getter reports))
(define-method (pay (e <employee>))
(salary e))
(define-method (pay (m <manager>))
(+ (next-method) (* 150 (length (reports m)))))
(define-method (describe-person (e <employee>))
(format #f "~a earns ~a" (name e) (pay e)))
(define-method (describe-person (m <manager>))
(format #f "~a and manages ~a" (next-method) (length (reports m))))
(define-method (initialize (e <employee>) initargs)
(next-method)
(when (negative? (salary e))
(error "negative salary for" (name e))))
(define ana (make <employee> #:name "Ana" #:salary 2100))
(define ben (make <employee> #:name "Ben" #:salary 1900))
(define eva (make <manager> #:name "Eva" #:salary 2800
#:reports (list ana ben)))
(for-each (lambda (e)
(display (describe-person e))
(newline))
(list ana ben eva))
(make <employee> #:name "Tom" #:salary -5)
The manager's pay takes the employee's pay from
next-method and adds 150 per report, and the manager's
describe-person appends to the employee's text, which already
uses the manager's pay. The initialize method first calls
next-method, which fills the slots from the init keywords,
and then checks the salary. Errors are described in the
exceptions article.
$ guile employees.scm Ana earns 2100 Ben earns 1900 Eva earns 3100 and manages 2 employees.scm:25:4: negative salary for "Tom"
Multiple dispatch
GOOPS selects a method by the classes of all its arguments, not only the first one. This is multiple dispatch. It suits operations that depend on two objects equally, such as collisions in a game.
(use-modules (oop goops) (ice-9 format))
(define-class <asteroid> ())
(define-class <ship> ())
(define-class <station> (<ship>))
(define-method (collide (a <asteroid>) (b <asteroid>))
"asteroids bounce off each other")
(define-method (collide (a <asteroid>) (s <ship>))
"the ship is damaged")
(define-method (collide (s <ship>) (a <asteroid>))
(collide a s))
(define-method (collide (a <ship>) (b <ship>))
"the ships dock")
(define-method (collide (a <asteroid>) (s <station>))
(string-append "the station's shield holds, but " (next-method)))
(define objects
(list (make <asteroid>) (make <ship>) (make <station>)))
(for-each
(lambda (a)
(for-each
(lambda (b)
(format #t "~10a ~10a ~a~%"
(class-name (class-of a)) (class-name (class-of b))
(collide a b)))
objects))
objects)
The classes have no slots; only their identity matters. A ship hitting
an asteroid reuses the asteroid–ship method by swapping the
arguments. A station is a ship, so two ships of any kind dock. The method
for an asteroid and a station is the most specific for that pair; its
next-method is the asteroid–ship method. A station
hitting an asteroid goes through the ship–asteroid method, which
swaps the arguments and so reaches the station method too. The column
widths in ~10a need the (ice-9 format) module.
$ guile dispatch.scm <asteroid> <asteroid> asteroids bounce off each other <asteroid> <ship> the ship is damaged <asteroid> <station> the station's shield holds, but the ship is damaged <ship> <asteroid> the ship is damaged <ship> <ship> the ships dock <ship> <station> the ships dock <station> <asteroid> the station's shield holds, but the ship is damaged <station> <ship> the ships dock <station> <station> the ships dock
Class slots and virtual slots
The #:allocation option controls where a slot is stored. A
class slot is shared by all instances, and a virtual slot stores nothing:
it computes its value with procedures.
(use-modules (oop goops))
(define-class <ticket> ()
(issued #:allocation #:class #:init-value 0 #:accessor issued)
(number #:getter ticket-number)
(event #:init-keyword #:event #:getter event))
(define-method (initialize (t <ticket>) initargs)
(next-method)
(set! (issued t) (+ (issued t) 1))
(slot-set! t 'number (issued t)))
(define-class <temperature> ()
(celsius #:init-keyword #:celsius #:init-value 0 #:accessor celsius)
(fahrenheit #:allocation #:virtual
#:slot-ref (lambda (t) (+ 32 (* 9/5 (celsius t))))
#:slot-set! (lambda (t f) (set! (celsius t) (* 5/9 (- f 32))))
#:accessor fahrenheit))
(define tickets
(map (lambda (e) (make <ticket> #:event e))
'("concert" "theatre" "concert")))
(for-each (lambda (t)
(format #t "#~a ~a~%" (ticket-number t) (event t)))
tickets)
(format #t "issued: ~a~%" (issued (car tickets)))
(define room (make <temperature> #:celsius 20))
(format #t "~a C is ~a F~%" (celsius room) (fahrenheit room))
(set! (fahrenheit room) 212)
(format #t "~a C is ~a F~%" (celsius room) (fahrenheit room))
(display (map slot-definition-name (class-slots <temperature>)))
(newline)
issued is one value for the whole class, so each new ticket
sees the count left by the previous one, and initialize gives
each ticket a number. The fahrenheit slot of a temperature is
computed from celsius by #:slot-ref, and setting
it with #:slot-set! changes celsius. Exact
arithmetic with 9/5 keeps whole numbers. class-slots lists
the virtual slot together with the real one.
$ guile class-slot.scm #1 concert #2 theatre #3 concert issued: 3 20 C is 68 F 100 C is 212 F (celsius fahrenheit)
Source
GOOPS — GNU Guile Reference Manual
Next-method — GNU Guile Reference Manual
In this article we have defined GOOPS classes with slots, created instances
with init keywords, read and written slots with getters and accessors,
added methods to generic functions, also for built-in types, defined how
objects are printed, used inheritance and next-method,
validated new instances in initialize, dispatched on two
arguments, and used class and virtual slots. The
object-oriented programming with closures
article builds the same ideas from closures, and the
records article defines plain data types.
Author
List all Scheme tutorials.