Congruence of lamda-lists of methods with their generic-function

Didier Verna didier at lrde.epita.fr
Sun Feb 9 09:10:10 UTC 2014


Jean-Claude Beaudoin <jean.claude.beaudoin at gmail.com> wrote:

> Hello CL Pros,
>
> I am trying to understand the meaning in conforming ANSI CL
> of the following code snippet and I am not quite sure what to think of it:
>
> (defgeneric foo (a b &key))
>
> (defmethod foo (a b &key c d) (or c d 42))
>
> At this point should a call to foo accept any keyword
> argument, no keyword argument or only :c and/or :d?
> What would be the outcome of this:
>
> (foo 1 2 :d 3)
>
> Is it an error being signaled or the value 3 being returned?
> I am inclined to believe that an error should be signaled,
> since the generic function lambda-list contains no
> explicit keyword argument and no &allow-other-keys.

I think that's the opposite. From CLHS:

| 7.6.5 Keyword Arguments in Generic Functions and Methods
| 
| When a generic function or any of its methods mentions &key in a lambda
| list, the specific set of keyword arguments accepted by the generic
| function varies according to the applicable methods. The set of keyword
| arguments accepted by the generic function for a particular call is the
| union of the keyword arguments accepted by all applicable methods and
| the keyword arguments mentioned after &key in the generic function
| definition, if any.

So because your method accepts :c and :d, then this particular call to
foo does as well. The critical thing to understand here is that the
valid keyword arguments may vary from one call to another.

-- 
Please help crowdfund my next Jazz quartet album !!
http://www.kisskissbankbank.com/quartet-the-second-album

Lisp, Jazz, Aïkido: http://www.didierverna.info



More information about the pro mailing list