Google 網路論壇不再支援新的 Usenet 貼文或訂閱項目,但過往內容仍可供查看。

generic functions

瀏覽次數:2 次
跳到第一則未讀訊息

Erik Naggum

未讀,
1998年3月6日 凌晨3:00:001998/3/6
收件者:

* Sam Steingold
| I hope it's clear what I want.

yes, you want generic functions to be different from what they are.

generic functions dispatch on the required parameters. all methods must
have congruent lambda lists to that of the generic function.

| 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).

why don't you use a single required argument in the first place? beats
me why you want to use generic functions and methods to begin with. is
it just to reuse a cool name?

#:Erik
--
God grant me serenity to accept the code I cannot change,
courage to change the code I can, and wisdom to know the difference.

Sunil Mishra

未讀,
1998年3月6日 凌晨3:00:001998/3/6
收件者:

In article <m3wwe7y...@mute.eaglets.com> Sam Steingold <s...@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


0 則新訊息