no-next-method
no-next-method Standard Generic Function
Syntax:
no-next-method generic-function method &rest args → {result}*
Method Signatures:
no-next-method (generic-function standard-generic-function)
(method standard-method)
&rest args
Arguments and Values:
generic-function – generic function to which method belongs.
method – method that contained the call to call-next-method for which there is no next method. args – arguments to call-next-method.
result—an object.
Description:
The generic function no-next-method is called by call-next-method when there is no next method.
The generic function no-next-method is not intended to be called by programmers. Programmers may write methods for it.
Exceptional Situations:
The system-supplied method on no-next-method signals an error of type error.
See Also:
call-next-methodExpanded Reference: no-next-method
Default Behavior
no-next-method is called when call-next-method is invoked but there is no next method in the chain. The default system-supplied method signals an error.
(defgeneric only-one (x))
(defmethod only-one ((x integer))
;; This is the only method -- call-next-method will fail
(call-next-method))
(handler-case (only-one 42)
(error () :caught-error))
=> :CAUGHT-ERROR
Avoiding the Error with next-method-p
The standard way to avoid no-next-method errors is to check with next-method-p before calling call-next-method.
(defgeneric safe-chain (x))
(defmethod safe-chain ((x number))
(if (next-method-p)
(call-next-method)
:end-of-chain))
(safe-chain 42)
=> :END-OF-CHAIN
Defining a Custom no-next-method Handler
You can define a method on no-next-method to customize the behavior when no next method exists.
(defgeneric chained-op (x))
(defmethod chained-op ((x integer))
(+ x (call-next-method)))
(defmethod no-next-method ((gf (eql #'chained-op)) method &rest args)
(declare (ignore gf method args))
0) ;; Return 0 as the base case
(chained-op 5)
=> 5