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

Pascal J. Bourguignon pjb at
Sun Feb 9 16:57:53 UTC 2014

Didier Verna <didier at> writes:

> Jean-Claude Beaudoin <jean.claude.beaudoin at> 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.


If you want to allow any kind of keywords for any call to the generic
function, you can declare it as:

    (defgeneric foo (a b &key &allow-other-keys))
    (defmethod foo (a b &key c d &allow-other-keys) (or c d 42))

or alternatively, if you're concerned only with a single call, you can
call it as:

   (foo unknown-object-1 unknown-object-2 :d 3 :allow-other-keys t)

__Pascal Bourguignon__
"Le mercure monte ?  C'est le moment d'acheter !"

More information about the pro mailing list