From: Sunil Mishra
Subject: Re: generic functions
Date: 
Message-ID: <efy4t1b1q00.fsf@peachtree.cc.gatech.edu>
In article <··············@mute.eaglets.com> Sam Steingold <···@usa.net> writes:

   The following signals an error:

   (defgeneric zz (&rest args)
    (:method ((x real) (y real) &optional (stream t))
	   (format stream "~a" (+ x y)))
    (:method ((z t) &optional (stream t)) (format t "~a" z)))

   *** - #<standard-method (#<built-in-class real> #<built-in-class real>)>
   has 2, but #<generic-function zz> has 0 required parameters

   I hope it's clear what I want. Is there a *good* way to do this in CL?
   (I don't want to check for the type of the first arg myself etc).

   Thanks.

As I understand CLOS, it doesn't make sense to supply types for
non-required arguments in specialized lambda lists. In other words, if you
have a method declaration like:

(defmethod foo ((x integer) &optional (y integer))
  ...)

the second value presented with the optional argument y is treated as a
default value, rather than a type specification. Only required arguments,
such as x, may be specialized. A generic function *needs* to have in its
lambda list all the required arguments over which it shall specialize. You
*really* can't specialize with an &rest...

In the end, the problem with your code is that the function zz needs *at
least* one required argument, and all the method definitions *must* have
the same required arguments. I don't know how things work with the optional
arguments, I would encourage you to check with the hyperspec or CLtL2.

This way, generic functions capture some abstractions and regularities in
your code. I can't imagine what you would be doing with the method
definitions you have described for zz, but consider what is required for
these method definitions. If the required arguments don't match up, I would
reconsider putting together a single unified definition of zz, since there
doesn't even appear to be enough in common to state what arguments are
common across the different methods.

Sunil