Structs and CLOS Basics
Once a program needs more structure than a bare list or hash table naturally provides — a record with named, typed fields — Common Lisp offers two tools at different levels of sophistication. defstruct gives you a lightweight, fast record type with auto-generated accessors and constructors, ideal for straightforward data aggregates. The Common Lisp Object System, CLOS, builds a full object-oriented layer on top with classes, inheritance, and generic functions whose behavior can be extended after the fact, without touching the original class definition.
Cricket analogy: A simple printed scorecard template with fixed labeled boxes (name, runs, balls) is like defstruct's fixed-field record, while a full player-management system with hierarchies of roles (batsman, all-rounder, wicketkeeper-batsman) that share and override behavior is like CLOS's class hierarchy.
defstruct: Fast, Simple Records
(defstruct person name age email) generates a constructor make-person, accessors person-name, person-age, and person-email, a predicate person-p, and a printer, all automatically. You create an instance with (make-person :name "Ada" :age 30 :email "ada@example.com") and read or write fields with (person-name p) or (setf (person-age p) 31). Structs are stored efficiently (typically as a vector-like block with a type tag) and dispatch on their slot accessors is direct and fast, but a struct's shape is essentially fixed once defined, and structs support only single inheritance via :include with no runtime redefinition flexibility.
Cricket analogy: Filling out a fixed printed team-sheet template with name, role, and batting-order slots for a new player mirrors make-person filling in a struct's predefined fields at construction.
(defstruct person
name
(age 0)
email)
(setf p (make-person :name "Ada" :age 30 :email "ada@example.com"))
(person-name p) ; => "Ada"
(setf (person-age p) 31)
(person-p p) ; => T
(person-p "not a person") ; => NILdefclass: Slots with Initforms and Initargs
CLOS classes are defined with defclass, where each slot can specify :initarg (the keyword used at construction), :initform (a default value, evaluated lazily per-instance if needed), and :accessor (which generates both a reader and a setf-able writer in one declaration). Unlike a struct, a class definition can be redefined at runtime and existing instances updated to match via update-instance-for-redefined-class, which is invaluable during interactive development — you can fix a class definition without restarting your program and losing all your live objects.
Cricket analogy: A player's registration profile with a default nationality (:initform) that's only overridden if specified (:initarg :nationality) at signing mirrors defclass's lazy default-with-override slot behavior.
(defclass account ()
((balance :initarg :balance :initform 0 :accessor account-balance)
(owner :initarg :owner :accessor account-owner)))
(setf a (make-instance 'account :owner "Ada" :balance 100))
(account-balance a) ; => 100
(incf (account-balance a) 50)
(account-balance a) ; => 150Generic Functions, Methods, and Dispatch
defmethod defines a method specialized on the class of one or more of its arguments — (defmethod describe-account ((a account)) ...) only applies when the first argument is (or inherits from) the account class. Calling describe-account triggers CLOS's dispatch mechanism, which selects the most specific applicable method for the actual runtime classes of the arguments; this is multiple dispatch, since specialization can occur on more than one argument at once, unlike single-dispatch OOP languages where only the receiving object's class matters.
Cricket analogy: An umpire's decision-review protocol differs depending on both which format (T20 vs Test) and which type of dismissal (LBW vs run-out) is in question — dispatching on two factors at once mirrors CLOS's multiple dispatch, unlike a rule that only checks one factor.
Because methods are attached to generic functions rather than sealed inside a class body, you can add a new defmethod for an existing generic function and an existing class in a completely separate file, without editing or recompiling the original class definition — this open extensibility is one of CLOS's most distinctive features compared to conventional single-dispatch class-based OOP.
Inheritance and Method Combination
A class can inherit from one or more superclasses by listing them where account () had an empty list above — (defclass savings-account (account) (...)) inherits account's slots and is eligible for account's methods too, and CLOS supports true multiple inheritance, resolving slot and method conflicts via a well-defined class precedence list. Inside a method, (call-next-method) explicitly invokes the next-most-general applicable method up the inheritance chain, letting a subclass method extend rather than fully replace its parent's behavior — for instance, a savings-account's withdraw method might check a minimum-balance rule and then call-next-method to perform the actual balance deduction defined on account.
Cricket analogy: A wicketkeeper-batsman inherits the base skills expected of any batsman (footwork, shot selection) but also adds keeping-specific duties, and when reviewing a dismissal they can defer to the general batting-review procedure before applying keeper-specific checks — mirroring call-next-method extending rather than replacing base behavior.
Forgetting call-next-method inside an :around or primary method that's meant to extend (not replace) inherited behavior is a common CLOS bug — it silently skips the entire rest of the applicable method chain, including base-class behavior your subclass may implicitly depend on, such as CLOS's own default slot-initialization logic in some designs.
- defstruct generates a fast, fixed-shape record type with auto-generated constructor, accessors, and predicate.
- defclass defines CLOS classes with slots specifying :initarg, :initform, and :accessor.
- Classes, unlike structs, can be redefined at runtime with existing instances updated to match.
- defmethod attaches specialized behavior to a generic function based on argument classes.
- CLOS supports multiple dispatch — specialization on more than one argument's class at once.
- Multiple inheritance is resolved via a well-defined class precedence list.
- call-next-method lets a subclass method extend rather than fully override inherited behavior.
Practice what you learned
1. What does (defstruct person name age) automatically generate?
2. What is the key structural difference between defstruct and defclass regarding runtime redefinition?
3. What does :initform specify in a defclass slot definition?
4. What is meant by CLOS's 'multiple dispatch'?
5. What does call-next-method do inside a defmethod body?
Was this page helpful?
You May Also Like
Vectors and Hash Tables in LISP
When to reach for vectors and hash tables instead of lists — constant-time indexed and keyed access versus linked-chain traversal.
List Manipulation Functions
The core built-in functions for navigating, searching, combining, and transforming lists, and the crucial distinction between destructive and non-destructive operations.
mapcar and Functional Iteration
How mapcar and its relatives let you transform lists by applying a function across them, without hand-writing explicit loops.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics