[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: find-method



> Date: Tue, 17 Mar 92 18:34:04
> From: cfry@MIT.EDU (Christopher Fry)
> 
> CLtL-2 has done an excellent job of making the description of 
> find-method obscure.
>
> 1. Is the 3rd argument suppose to be a list of class names?

It clearly says that argument is a list of "parameter specializers", a formal 
term used all over the place in the CLOS specification.  And just what is a 
"parameter specializer" you ask?  Why that's easy.  It's defined right here on 
page ... hmm, no, maybe its on page ... Well, gosh.  It was here just a minute 
ago.

Seriously, a parameter specializer is either a class metaobject (*not* a name) 
or an EQL type specifier.  There are a couple of different passages from which 
that information may be infered (see the two bullets at the bottom of p.789, 
for example), but I don't see an explicit definition anywhere.  (Fortunately, 
the X3J13 draft has most terms of this sort moved to a glossary, making it 
harder for one to sneak through without an explicit definition being attached 
to it).

> 2. Regardless of whether the last arg [errorp] is t or nil, when I call
>    find-method in MCL2 it errors. Perhaps I just don't have the
>    other args right. Can somebody please provide an example 
>    of calling this fn?

In MCL2.0f2c5

  ? (defclass test-class () ())
  #<STANDARD-CLASS TEST-CLASS>
  ? (defgeneric test-function (x y)
      (:method ((x test-class) (y (eql 5))) (list x y))
      (:method ((x (eql 5)) (y test-class)) (list x y)))
  #<STANDARD-GENERIC-FUNCTION TEST-FUNCTION #xD58EAE>
  ? (list
     (find-method #'test-function () `(,(find-class 'test-class) (eql 5)))
     (find-method #'test-function () `((eql 5) ,(find-class 'test-class)))
     (find-method #'test-function () '((eql 5) (eql 5)) nil))
  (#<STANDARD-METHOD TEST-FUNCTION (TEST-CLASS (EQL 5))>
   #<STANDARD-METHOD TEST-FUNCTION ((EQL 5) TEST-CLASS)>
   NIL)

> 3. What I really want to find out is: given an instance and the symbol name 
>    of a method, Is (meth-name inst) going to error due to the lack of 
>    meth-name existing?  Is find-method the correct fn to be calling?

No.  To do something like that you need to use compute-applicable-methods (see 
CLtL2, p.822).  For example,

  ? (defclass test-subclass (test-class) ())
  #<STANDARD-CLASS TEST-SUBCLASS>
  ? (defparameter *test-instance* (make-instance 'test-class))
  *TEST-INSTANCE*
  ? (defparameter *test-sub-instance* (make-instance 'test-subclass))
  *TEST-SUB-INSTANCE*
  ? (compute-applicable-methods #'test-function `(,*test-instance* 5))
  (#<STANDARD-METHOD TEST-FUNCTION (TEST-CLASS (EQL 5))>)
  ? (compute-applicable-methods #'test-function `(5 ,*test-sub-instance*))
  (#<STANDARD-METHOD TEST-FUNCTION ((EQL 5) TEST-CLASS)>)
  ? (find-method #'test-function
                 ()
                 `((eql 5) ,(class-of *test-sub-instance*))
                 nil)
  NIL

The difference between compute-applicable-methods and find-method can be seen 
in the last two calls.  find-method looks for a method which is exact match to 
the specializers and qualifiers.