4

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.

Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346
sheikh_anton
  • 3,422
  • 2
  • 15
  • 23

2 Answers2

8

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.

Tim
  • 502
  • 4
  • 10
6

Common Lisp object model is based on the notion of a generic function, so methods are attached to GFs, not to ordinary objects, see generic-function-methods (it is in MOP, not ANSI CL, so you need to find the package it lives in using apropos or find-all-symbols).

The Common Lisp Object System has a very powerful MetaObject Protocol. You can use it to view (and often modify!) a lot of internal information about your objects and functions.

sds
  • 58,617
  • 29
  • 161
  • 278