Skip to main content

add-method

add-method Standard Generic Function

Syntax:

add-method generic-function method ! generic-function

Method Signatures:

add-method (generic-function standard-generic-function)

(method method)

Arguments and Values:

generic-function—a generic function object.

method—a method object.

Description:

The generic function add-method adds a method to a generic function.

If method agrees with an existing method of generic-function on parameter specializers and qualifiers, the existing method is replaced.

Exceptional Situations:

The lambda list of the method function of method must be congruent with the lambda list of generic-function, or an error of type error is signaled.

If method is a method object of another generic function, an error of type error is signaled.

See Also:

defmethod, defgeneric, find-method, remove-method, Section 7.6.3 (Agreement on Parameter Specializers and Qualifiers)

Expanded Reference: add-method

Adding a Method Programmatically

add-method adds a method object to a generic function. This is the low-level operation that defmethod uses internally. It is mainly useful for metaprogramming.

(defgeneric greet (who))

;; find-method after defmethod
(defmethod greet ((who string))
(format nil "Hello, ~A!" who))

;; The method is now on the generic function
(greet "World")
=> "Hello, World!"

Replacing an Existing Method

If a method with the same specializers and qualifiers already exists, add-method replaces it.

(defgeneric describe-number (n))

(defmethod describe-number ((n integer))
"an integer")

(describe-number 42)
=> "an integer"

;; Redefining with defmethod replaces via add-method internally
(defmethod describe-number ((n integer))
"a whole number")

(describe-number 42)
=> "a whole number"

Using add-method with find-method and remove-method

You can combine add-method with find-method and remove-method for dynamic method manipulation.

(defgeneric op (x))

(defmethod op ((x number)) (* x 2))

(op 5)
=> 10

;; Find and remove the method
(let ((m (find-method #'op '() (list (find-class 'number)))))
(remove-method #'op m))

;; Now no method is applicable for numbers
(handler-case (op 5)
(error () "no method"))
=> "no method"