3.4 The Common Lisp Object System

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:

Here’s the first line of a sample entry from the API Reference chapter:

Method: find-if (t set)  pred s &key key

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