Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List object methods in Common Lisp (CLOS)

Is there any way to get all methods defined for object and check if object responds to specified method?

Looking for something like Ruby's "foo".methods

(list-methods *myobj*) ;; -> (method0 method1 methodN)

And also something like ruby's "foo".respond_to? :method

(has-method-p *myobj* 'foo-method) ;; -> T

For slots there is slot-exists-p, what's for methods?

Thanks.

like image 575
coredump Avatar asked Dec 08 '22 07:12

coredump


1 Answers

You can use the MOP function SPECIALIZER-DIRECT-GENERIC-FUNCTIONS to find all the generic functions that contain a method that specializes specifically on a class, which is close to what you are asking for in Ruby. You can actually find all the generic functions that specialize on a class or any of its superclasses with (for SBCL; for other implementations check out closer-mop or the implementation's mop package):

(defun find-all-gfs (class-name)
  (remove-duplicates
   (mapcan (lambda (class)
             (copy-list (sb-mop:specializer-direct-generic-functions class)))
           (sb-mop:compute-class-precedence-list (find-class class-name)))))

The problem with this function is that many built-in generic functions specialize on the universal supertype T, so in SBCL you get a list of 605 generic functions which might not be all that interesting. However, you can build some interesting tools with this general approach e.g., by filtering the list of superclasses or generic functions based on package.

like image 98
Tim Avatar answered Dec 28 '22 01:12

Tim