Scheme Expert · Lesson 1 · On the house

Classes and generic functions with GOOPS

Define classes with slot options, dispatch generic functions on argument classes, chain with next-method, and inspect classes at run time. Lesson 1 is free — on the house.

Lessons · Scheme Expert · Lesson 1 of 10 · On the house

Classes and generic functions with GOOPS

Define classes with slot options, dispatch generic functions on argument classes, chain with next-method, and inspect classes at run time.

Illustration for Scheme
Lesson 1 is on the house.

(oop goops) is Guile's object system. (define-class <circle> (<shape>) slot …) names a class, lists its superclasses, then its slots. Class names are ordinary variables, conventionally wrapped in angle brackets.

Each slot takes options. #:init-keyword #:r lets make fill it, #:init-value 1 supplies a default, #:getter makes a read-only generic, and #:accessor makes a getter that also works with set!, as in (set! (radius c) 3).

(make <circle> #:name "c" #:r 2) allocates an instance. (slot-ref obj 'w) and (slot-set! obj 'w 5) read and write a slot by name, with or without an accessor defined.

Methods live in generic functions, not in classes. (define-generic area) creates one; (define-method (area (c <circle>)) …) adds a method specialised on <circle>. define-method also creates the generic if it does not exist yet.

A call picks the most specific applicable method, using the class precedence list of each argument. Inside a method, (next-method) calls the next less specific one with the same arguments, so describe on a circle prefixes "round " and then reuses the <shape> method.

(is-a? obj class) tests membership including superclasses. (class-of obj) returns the class, (class-name class) its symbol, and (class-direct-supers class) the parents named in define-class.

goops

(use-modules (oop goops))
(define-class <shape> ()
  (name #:init-keyword #:name #:getter shape-name))
(define-class <circle> (<shape>)
  (r #:init-keyword #:r #:init-value 1 #:accessor radius))
(define-class <rect> (<shape>)
  (w #:init-keyword #:w) (h #:init-keyword #:h))
(define-generic area)
(define-method (area (c <circle>)) (* 3 (radius c) (radius c)))
(define-method (area (r <rect>)) (* (slot-ref r 'w) (slot-ref r 'h)))
(define-method (describe (s <shape>))
  (string-append (shape-name s) ":" (number->string (area s))))
(define-method (describe (c <circle>))
  (string-append "round " (next-method)))
(define c (make <circle> #:name "c" #:r 2))
(define r (make <rect> #:name "r" #:w 3 #:h 4))
(display (area c)) (newline)
(display (area r)) (newline)
(set! (radius c) 3)
(display (area c)) (newline)
(slot-set! r 'w 5)
(display (slot-ref r 'w)) (newline)
(display (describe r)) (newline)
(display (describe c)) (newline)
(display (is-a? c <shape>)) (newline)
(display (class-name (class-of r))) (newline)
(display (map class-name (class-direct-supers <circle>))) (newline)
(display (radius (make <circle> #:name "d"))) (newline)
Match this when you type.

Quiz

Which slot option creates a procedure that both reads the slot and works with set!?

Quiz

Where are GOOPS methods stored?

Quiz

What does (next-method) do inside a method?

Quiz

What does (is-a? c <shape>) return for an instance of <circle>, a subclass of <shape>?

Check

Match the still for: Classes and generic functions with GOOPS.

Open-book: the answers are on this page. Pass every quiz and check (5) to mark the lesson done. This visit: 0/5.