This section is intended for readers unfamiliar with Common Lisp. It is not a full treatment of CLOS, the Common Lisp Object System, but should provide enough background that you can understand the rest of this book.
CLOS is unusual, among object-oriented languages, in several ways:
Imagine a hierarchy of classes with methods in a mainstream OO language. For each method, either it overrides a method of the same name in a superclass, or it does not; the latter we call root methods. Consider a single root method and all the subclass methods that override it; that’s the set of methods that forms a generic function.
The language spec has a detailed definition of what “best match” means here, but the short version is that since there are subtyping relationships involved, we want a method whose specializers are more specific to shadow one whose specializers are less specific, when the arguments are of the more specific types. In the single-dispatch case, this generates the familiar overriding behavior of other OO languages: a method on a subclass overrides one of the same name on a superclass.
t is the top of the
hierarchy, the class of all objects. When no specializer is explicitly given for a parameter of a
method, the specializer defaults to t.
CLOS also provides a way to specialize a method parameter on a symbol, so that the method will be
called only when that specific symbol is passed. FSet uses this for its generic function
convert, to specify the intended conversion.
Here’s the first line of a sample entry from the API Reference chapter:
Here find-if is the generic function, and (t set) is the list of specializers. This
method will be invoked on a call whose second argument is a set. The required parameters are
pred and s; s will be bound to the set.
For brevity, I have usually omitted trailing t specializers in this book, since they have no
effect on method selection.
Oh, maybe I should say a little about optional and keyword parameters. CL has both of these, marked
in parameter lists by &optional and &key respectively. Optional parameters match
arguments by position; for keyword parameters, the argument must be preceded by the parameter name
as a keyword symbol (i.e., preceded by a colon). Both optional and keyword parameters may be
given default values in the parameter list; the default default is nil. Examples:
(defun foo (x &optional y (z 0)) ...) (defun bar (x &key y (z 3)) ...) ;;; some possible calls (foo 17) ; x = 17, y = nil, z = 0 (foo 17 42) ; x = 17, y = 42, z = 0 (foo 17 42 19) ; x = 17, y = 42, z = 19 (bar 22) ; x = 22, y = nil, z = 3 (bar 22 :z 7) ; x = 22, y = nil, z = 7 (bar 22 :z 7 :y 13) ; x = 22, y = 13, z = 7