Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 38 ยท CLOS Object System

CLOS Inheritance and Method Combination

Understand CLOS inheritance and the class precedence list, call-next-method, :before, :after and :around auxiliary methods, multiple inheritance and mixins in Common Lisp.

When several methods apply to a call, CLOS does not choose just one and forget the rest. It sorts the applicable methods from most to least specific and combines them according to rules. Understanding this makes mixins, logging, validation and caching almost effortless.

CLOS code is read-only (no Common Lisp runtime on this site). The JavaScript examples are labelled stand-ins that implement the same call order by hand.

Inheritance and the class precedence list

A class lists its superclasses in defclass, and CLOS may use more than one:

LISP
(defclass animal () ((name :initarg :name :reader name)))
(defclass pet (animal) ())
(defclass guard (animal) ())
(defclass guard-dog (pet guard) ())

Every class has a class precedence list (CPL): a linear order from the class itself to standard-object and t, respecting the order in which superclasses were listed and every class appearing before its own superclasses. For guard-dog it is (guard-dog pet guard animal standard-object t). (class-precedence-list (find-class 'guard-dog)) shows it, and the list is what "most specific" means when methods are sorted.

flowchart TD
    GD["guard-dog"] --> P["pet"]
    GD --> G["guard"]
    P --> A["animal"]
    G --> A
    A --> SO["standard-object"]
    SO --> T["t"]

Precedence runs guard-dog, pet, guard, animal, standard-object, t: pet comes before guard because it was listed first.

call-next-method

Inside a method body, (call-next-method) runs the next most specific method, with the same arguments by default (or new ones if you pass them). This is the CLOS equivalent of super:

LISP
(defgeneric describe-animal (a))
(defmethod describe-animal ((a animal))
  (format nil "~a is an animal" (name a)))
(defmethod describe-animal ((a pet))
  (format nil "~a; also a pet" (call-next-method)))
(defmethod describe-animal ((a guard-dog))
  (format nil "~a; also a guard dog" (call-next-method)))

(describe-animal (make-instance 'guard-dog :name "Rex"))
;; => "Rex is an animal; also a pet; also a guard dog"

guard-dog's method calls pet's, which calls animal's. Note that the order shown (animal, then pet, then guard-dog) comes from the string building, not from execution order: execution goes down the chain and the formatting runs on the way back. next-method-p tells you whether a next method exists.

Standard method combination

For ordinary methods (primary methods), only the most specific runs unless it calls call-next-method. You can add three kinds of auxiliary methods with qualifiers:

Qualifier When it runs
:before before the primary method, most specific first
:after after the primary method, most specific last (reverse order)
:around wraps everything, most specific outermost; must call call-next-method to continue
LISP
(defgeneric deposit (account amount))

(defmethod deposit ((a account) amount)
  (incf (balance a) amount))

(defmethod deposit :before ((a account) amount)
  (when (<= amount 0) (error "Deposit must be positive: ~a" amount)))

(defmethod deposit :after ((a account) amount)
  (format t "balance is now ~a~%" (balance a)))

(defmethod deposit :around ((a account) amount)
  (format t "start~%")
  (prog1 (call-next-method)
    (format t "end~%")))

The full order for one call is: :around (before its call-next-method), :before methods, the primary, :after methods, then the rest of the :around. Return values come from the primary method (and the :around method if it chooses to return something else). :before and :after methods cannot change the result and cannot call call-next-method.

JS Stand-in: order of around, before, primary, after

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice in the output that :after runs account first and savings last, the reverse of :before: cleanup mirrors setup.

Mixins

Multiple inheritance in CLOS is typically used for mixins, small classes that add one behavior and are combined as needed. Auxiliary methods make mixins composable without knowing about each other:

LISP
(defclass logged-mixin () ())

(defmethod deposit :before ((x logged-mixin) amount)
  (format t "LOG: deposit ~a~%" amount))

(defclass audited-account (logged-mixin account) ())

audited-account gets logging for free. Nothing in account needed to change. Good mixin classes have no slots of their own, or only a few, and use :before, :after and :around methods rather than primaries.

Other combinations

The standard combination is the default. Others are built in and selected in defgeneric:

LISP
(defgeneric total-cost (item)
  (:method-combination +))

(defmethod total-cost + ((x car))   1000)
(defmethod total-cost + ((x vehicle)) 200)

With (:method-combination +) all applicable methods run and their results are added. and, or, list, append, max, min, progn and nconc are also available, and you can define your own with define-method-combination.

JS Stand-in: combining results with +

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does (call-next-method) do inside a method?

    It continues along the sorted list of applicable methods, with the same arguments unless you pass new ones.
  2. 2. In what order do :after methods run?

    :before goes most specific first; :after goes in the opposite direction so cleanup mirrors setup.
  3. 3. Which method must call call-next-method for the primary method to run?

    An :around method controls whether the rest of the chain runs by calling or not calling call-next-method.
  4. 4. What is a mixin class typically?

    Mixins add a feature (logging, caching, validation) through auxiliary methods and are combined where needed.

Summary

  • Each class has a class precedence list that orders methods from specific to general.
  • call-next-method continues along that list; next-method-p tests for another method.
  • :before, :after and :around methods wrap the primary method with a fixed, predictable order.
  • Mixins use auxiliary methods to add behavior without touching existing classes.
  • Other method combinations (+, list, and, ...) combine all applicable methods' results.

Next: Initialization, Slots and a Glimpse of the MOP