Is there a way to extract a list of methods from a generic function in Common Lisp?
For example:
(defmethod say ((self string)) ; method-0
(format t "Got string: ~a~%" self))
(defmethod say ((self integer)) ; method-1
(format t "Got integer: ~a~%" self))
(defmethod say ((self symbol)) ; method-2
(format t "Got symbol: ~a~%" self))
(extract-methods-from-generic 'say) ; -> (method-0-obj method-1-obj method-2-obj)
To be more specific, I'm targeting ECL, so if this can be done via C API - that's ok.
I need this to do the next trick:
(defgeneric merged-generic ())
(loop for method
in (extract-methods-from-generic 'some-generic-0)
do (add-method merged-generic method))
(loop for method
in (extract-methods-from-generic 'some-generic-1)
do (add-method merged-generic method))
The generic function
generic-function-methodsis available in the CLOS to get all the methods of a generic function (see the CLOS protocol), but note that, for the second part of your question, you can attach a method to a generic function (withadd-method) only if the method is detached from any other generic function (see the protocol):You can use both of those functions through the package closer-mop, independently from any implementation: