Definitions in Lisp
Table of Contents
1. Overview
Common Lisp — being a Lisp-2 — has two namespaces for definitions:
one for functions, the other for values. How does this work? And how
does, say, defgeneric
and defmethod
work?
2. Defun
Usually defun
is a macro.
2.1. SBCL
2.2. ABCL
Armed Bear CL has a primitive %defun Java class.
3. Defgeneric
3.1. SBCL
SBCL uses a rather contrived macro roughly 90 lines long.
It boils down to delegating the binding to ensure-generic-function.
If we call defgeneric
on an existing function, it attempts to redefine
that function as a generic by clearing the existing definition and
continuing on as if there were no definition; otherwise, it just
calls ensure-generic-function-using-class (CLHS for ensure-generic-function
). This then
amounts to (setf (%funcallable-instance-fun name) ...)
.
3.2. ABCL
Armed Bear uses a macro. It delegates to %defgeneric
(L1786-L1802).
which is a wrapper around ensure-generic-function
(CLHS, implementation).
3.3. CCL
CCL has a macro defining defgeneric
. Like ABCL, it delegates to
%defgeneric which is a wrapper around ensure-generic-function.