Skip to main content

compute-applicable-methods

compute-applicable-methods Standard Generic Function

Syntax:

compute-applicable-methods generic-function function-arguments → methods

Method Signatures:

compute-applicable-methods (generic-function standard-generic-function)

Arguments and Values:

generic-function—a generic function.

function-arguments—a list of arguments for the generic-function.

methods—a list of method objects.

Description:

Given a generic-function and a set of function-arguments, the function compute-applicable-methods returns the set of methods that are applicable for those arguments sorted according to precedence order. See Section 7.6.6 (Method Selection and Combination).

Affected By:

defmethod

See Also:

Section 7.6.6 (Method Selection and Combination)

Expanded Reference: compute-applicable-methods

Finding Methods for Given Arguments

compute-applicable-methods takes a generic function and a list of arguments, and returns the list of methods that would be applicable for those arguments, sorted by precedence.

(defgeneric speak (animal))

(defmethod speak ((a string))
(format nil "~A speaks" a))

(defmethod speak ((a number))
(format nil "Number ~A" a))

(let ((methods (compute-applicable-methods #'speak '("hello"))))
(length methods))
=> 1

Inspecting Method Ordering

The returned list is in precedence order, most specific first. This shows the actual method dispatch order.

(defclass a () ())
(defclass b (a) ())
(defclass c (b) ())

(defgeneric process-obj (obj))

(defmethod process-obj ((obj a)) :a)
(defmethod process-obj ((obj b)) :b)
(defmethod process-obj ((obj c)) :c)

(let* ((obj (make-instance 'c))
(methods (compute-applicable-methods #'process-obj (list obj))))
(length methods))
=> 3
;; The methods are in order: C, B, A (most specific first)

No Applicable Methods

When no methods match, an empty list is returned.

(defgeneric typed-op (x))
(defmethod typed-op ((x string)) x)

(compute-applicable-methods #'typed-op '(42))
=> NIL

Checking Qualifiers on Applicable Methods

You can combine compute-applicable-methods with method-qualifiers to inspect which qualified methods would apply.

(defgeneric do-work (x))
(defmethod do-work ((x number)) x)
(defmethod do-work :before ((x integer)) (format t "before~%"))
(defmethod do-work :after ((x integer)) (format t "after~%"))

(let ((methods (compute-applicable-methods #'do-work '(42))))
(mapcar #'method-qualifiers methods))
;; => ((:AFTER) (:BEFORE) NIL)