next-method-p
next-method-p Local Function
Syntax:
next-method-p ⟨no arguments⟩ → generalized-boolean
Arguments and Values:
generalized-boolean—a generalized boolean.
Description:
The locally defined function next-method-p can be used within the body forms (but not the lambda list) defined by a method-defining form to determine whether a next method exists.
The function next-method-p has lexical scope and indefinite extent.
Whether or not next-method-p is fbound in the global environment is implementation-dependent; however, the restrictions on redefinition and shadowing of next-method-p are the same as for symbols in the COMMON-LISP package which are fbound in the global environment. The consequences of attempting to use next-method-p outside of a method-defining form are undefined.
See Also:
call-next-method, defmethod, call-method
Expanded Reference: next-method-p
Checking for a Next Method
next-method-p returns true if there is a next method to call via call-next-method, and false otherwise. It takes no arguments.
(defclass base () ())
(defclass derived (base) ())
(defgeneric greet (obj))
(defmethod greet ((obj base))
(if (next-method-p)
(format nil "Base (more methods exist)")
(format nil "Base (no more methods)")))
(defmethod greet ((obj derived))
(if (next-method-p)
(format nil "Derived -> ~A" (call-next-method))
(format nil "Derived (no more methods)")))
(greet (make-instance 'derived))
=> "Derived -> Base (no more methods)"
(greet (make-instance 'base))
=> "Base (no more methods)"
Safely Avoiding no-next-method Errors
Without next-method-p, calling call-next-method when there is no next method results in no-next-method being invoked (which signals an error by default).
(defgeneric safe-process (obj))
(defmethod safe-process ((obj t))
(if (next-method-p)
(call-next-method)
:default-result))
(safe-process 42)
=> :DEFAULT-RESULT
Using next-method-p in :around Methods
In :around methods, next-method-p indicates whether the standard method chain (:before, primary, :after) can proceed.
(defgeneric handle (thing))
(defmethod handle :around ((thing number))
(if (next-method-p)
(progn
(format t "Proceeding with number handling~%")
(call-next-method))
(format t "No primary method for numbers~%")))
(defmethod handle ((thing integer))
(format nil "Handling integer: ~A" thing))
(handle 42)
.. Proceeding with number handling
..
=> "Handling integer: 42"