From: David J. Cooper
Subject: Querying whether there is a method which is applicable for an arglist
Date: 
Message-ID: <38A2CCC8.1F9F8FE1@genworks.com>
Hello,

Given a generic function and an arglist, what is the best mechanism
to determine whether there is a corresponding method which applies
to the given arglist?

Something like: 

 (applies-p function arglist)
 -> T or NIL

My understanding is that CLOS builds a table dynamically to cache
this kind of information, so there might be a bit of overhead the
first time something like this is called, but when called 
subsequently with the same function and arglist types (or when
the actual method is called) it should be a fairly lightweight
operation.

The current solution is to do something like:

 (multiple-value-bind (value error)
    (ignore-errors (apply function arglist)))

and inspect the resulting error and proceed accordingly. This works 
but has obvious drawbacks, including the fact that, as a general
rule,  I don't like using ignore-errors.

I should add that 90% of the time the function *will* apply for
the arglist, so probably an optimistic mechanism should be used
which has the least cost when the function does apply (and maybe
this is in fact the ignore-errors approach, since the majority
of the time the ignore-errors just behaves as a progn and we
just proceed normally with the returned value).

One obvious drawback of the above ignore-errors approach is
limitation in dealing with methods which return more than two
return-values.

Thanks,

 -dave

-- 
David J. Cooper Jr, Chief Engineer	Genworks International
·······@genworks.com			5777 West Maple, Suite 130
(248) 932-2512 (Genworks HQ/voicemail)	West Bloomfield, MI 48322-2268
(248) 407-0633 (pager)			http://www.genworks.com

From: Nichael Cramer
Subject: Re: Querying whether there is a method which is applicable for an arglist
Date: 
Message-ID: <38A2D169.35704B7C@sover.net>
David J. Cooper wrote:

> Hello,
>
> Given a generic function and an arglist, what is the best mechanism
> to determine whether there is a corresponding method which applies
> to the given arglist?
> [...]
> My understanding is that CLOS builds a table dynamically to cache
> this kind of information, so there might be a bit of overhead the
> first time something like this is called, but when called
> subsequently with the same function and arglist types (or when
> the actual method is called) it should be a fairly lightweight
> operation.
>
> The current solution is to do something like:
>
>  (multiple-value-bind (value error)
>     (ignore-errors (apply function arglist)))
>
> and inspect the resulting error and proceed accordingly. This works
> but has obvious drawbacks, including the fact that, as a general
> rule,  I don't like using ignore-errors.
>
> I should add that 90% of the time the function *will* apply for
> the arglist, so probably an optimistic mechanism should be used
> which has the least cost when the function does apply (and maybe
> this is in fact the ignore-errors approach, since the majority
> of the time the ignore-errors just behaves as a progn and we
> just proceed normally with the returned value).

First off, it appears, from your example, that the rest of your code would
be (reasonably) happy if the method didn't get called (i.e. in the case
for which there is not a method defined with the appropriate args).

In that case, a reason approach --much in the spirit of CLOS-- would be to
write a default method for the generic function(s) in question.  It's
arguments would all be unqualified and it would basically be a no-op,
(possibly printing out a warning tell you that this has happened).

> One obvious drawback of the above ignore-errors approach is
> limitation in dealing with methods which return more than two
> return-values.

In this case, the default method could, perhaps, return (default) values
that are "friendlier" to your program.

N


Nichael Cramer
home: ·······@sover.net
http://www.sover.net/~nichael
From: Fernando D. Mato Mira
Subject: Re: Querying whether there is a method which is applicable for an  arglist
Date: 
Message-ID: <38A2E183.4DDC49FC@iname.com>
Nichael Cramer wrote:

> David J. Cooper wrote:
>
> > Hello,
> >
> > Given a generic function and an arglist, what is the best mechanism
> > to determine whether there is a corresponding method which applies
> > to the given arglist?

(compute-applicable-methods function arglist)


--
Fernando D. Mato Mira
Real-Time SW Eng & Networking
Advanced Systems Engineering Division
CSEM
Jaquet-Droz 1                   email: matomira AT acm DOT org
CH-2007 Neuchatel                 tel:       +41 (32) 720-5157
Switzerland                       FAX:       +41 (32) 720-5720

www.csem.ch      www.vrai.com     ligwww.epfl.ch/matomira.html
From: David J. Cooper
Subject: Yes, that's exactly what I was looking for (was: Re: Querying whether  there is a method which is applicable for an arglist)
Date: 
Message-ID: <38A3354B.F9AE7235@genworks.com>
"Fernando D. Mato Mira" wrote:
> 
> 
> David J. Cooper wrote:
>
> > Given a generic function and an arglist, what is the best mechanism
> > to determine whether there is a corresponding method which applies
> > to the given arglist?
> 
> (compute-applicable-methods function arglist)
> 

Yes! this is exactly what I was looking for. Sorry to have bothered the
newsgroup with it, I obviously didn't do enough homework on this one 
before posting -- it is listed plain as day in the reference section
of my autographed copy of Graham's _ANSI_Common_Lisp_.

Thanks again.

 -dave

-- 
David J. Cooper Jr, Chief Engineer	Genworks International
·······@genworks.com			5777 West Maple, Suite 130
(248) 932-2512 (Genworks HQ/voicemail)	West Bloomfield, MI 48322-2268
(248) 407-0633 (pager)			http://www.genworks.com
From: Robert Monfera
Subject: Re: Querying whether there is a method which is applicable for an arglist
Date: 
Message-ID: <38A2DA3E.8EB65679@fisec.com>
"David J. Cooper" wrote:
>
> Hello,
>
> Given a generic function and an arglist, what is the best mechanism
> to determine whether there is a corresponding method which applies
> to the given arglist?

Maybe (clos:compute-applicable-methods generic-function arguments) would
work for you, if your implementation has MOP.

Robert
From: Erik Naggum
Subject: Re: Querying whether there is a method which is applicable for an arglist
Date: 
Message-ID: <3159202464932715@naggum.no>
* Robert Monfera <·······@fisec.com>
| Maybe (clos:compute-applicable-methods generic-function arguments) would
| work for you, if your implementation has MOP.

  COMPUTE-APPLICABLE-METHODS is in the COMMON-LISP package in ANSI Common
  Lisps, and is unrelated to whether the implementation has MOP.

#:Erik
From: Robert Monfera
Subject: Re: Querying whether there is a method which is applicable for an arglist
Date: 
Message-ID: <38A33E12.EBCEB621@fisec.com>
Erik Naggum wrote:

>   COMPUTE-APPLICABLE-METHODS is in the COMMON-LISP package [...]

Good catch, I'm a little surprised.  Can someone tell me an example
where and how this is useful outside the context of MOP?  From all the
COMPUTE- functions and numerous introspective functions of MOP only this
one is included in CL.

Robert
From: Fernando D. Mato Mira
Subject: Re: Querying whether there is a method which is applicable for anarglist
Date: 
Message-ID: <38A3F2F1.2DD5805D@iname.com>
Robert Monfera wrote:

> Erik Naggum wrote:
>
> >   COMPUTE-APPLICABLE-METHODS is in the COMMON-LISP package [...]
>
> Good catch, I'm a little surprised.  Can someone tell me an example
> where and how this is useful outside the context of MOP?  From all the
> COMPUTE- functions and numerous introspective functions of MOP only this
> one is included in CL.
>
> Robert

Maybe the standard would have to say "FOR INTROSPECTIVE USE ONLY" [for now
;->]

http://www.alu.org/HyperSpec/Issues/iss072-writeup.html

--
Fernando D. Mato Mira
Real-Time SW Eng & Networking
Advanced Systems Engineering Division
CSEM
Jaquet-Droz 1                   email: matomira AT acm DOT org
CH-2007 Neuchatel                 tel:       +41 (32) 720-5157
Switzerland                       FAX:       +41 (32) 720-5720

www.csem.ch      www.vrai.com     ligwww.epfl.ch/matomira.html
From: Tim Bradshaw
Subject: Re: Querying whether there is a method which is applicable for an 	arglist
Date: 
Message-ID: <ey3puu0yf45.fsf@cley.com>
* Robert Monfera wrote:

> Good catch, I'm a little surprised.  Can someone tell me an example
> where and how this is useful outside the context of MOP?  From all the
> COMPUTE- functions and numerous introspective functions of MOP only this
> one is included in CL.

Without thinking too hard, I'd guess that this is one of the places
where the border is a little ragged between what got in and what
didn't.  Perhaps there is some deeper reason though.

--tim