Here, as elsewhere,
175
Technically you could skip the DEFGENERIC
altogether—if you define a method with DEFMETHOD
and no such generic function has been defined, one is automatically created. But it's good form to define generic functions explicitly, if only because it gives you a good place to document the intended behavior.
176
A method can 'accept' &key
and &rest
arguments defined in its generic function by having a &rest
parameter, by having the same &key
parameters, or by specifying &allow-other- keys
along with &key
. A method can also specify &key
parameters not found in the generic function's parameter list— when the generic function is called, any &key
parameter specified by the generic function or any applicable method will be accepted.
177
CALL-NEXT-METHOD
is roughly analogous to invoking a method on super
in Java or using an explicitly class-qualified method or function name in Python or C++.
178
While building the effective method sounds time-consuming, quite a bit of the effort in developing fast Common Lisp implementations has gone into making it efficient. One strategy is to cache the effective method so future calls with the same argument types will be able to proceed directly.
179
Actually, the order in which specializers are compared is customizable via the DEFGENERIC
option :argument-precedence-order
, though that option is rarely used.
180
In languages without multimethods, you must write dispatching code yourself to implement behavior that depends on the class of more than one object. The purpose of the popular Visitor design pattern is to structure a series of singly dispatched method calls so as to provide multiple dispatch. However, it requires one set of classes to know about the other. The Visitor pattern also quickly bogs down in a combinatorial explosion of dispatching methods if it's used to dispatch on more than two objects.
181
Defining new methods for an existing class may seem strange to folks used to statically typed languages such as C++ and Java in which all the methods of a class must be defined as part of the class definition. But programmers with experience in dynamically typed object-oriented languages such as Smalltalk and Objective C will find nothing strange about adding new behaviors to existing classes.
182
In other object-oriented languages, slots might be called
183
As when naming functions and variables, it's not quite true that you can use
184
The argument to MAKE-INSTANCE
can actually be either the name of the class or a class object returned by the function CLASS-OF
or FIND-CLASS
.
185
Another way to affect the values of slots is with the :default-initargs
option to DEFCLASS
. This option is used to specify forms that will be evaluated to provide arguments for specific initialization parameters that aren't given a value in a particular call to MAKE-INSTANCE
. You don't need to worry about :default-