Date: Tue, 14 Nov 2000 12:15:23 +0100 (CET)
From: David Kaasen <nvg.ntnu.no at kaasen>
Content-Type: TEXT/PLAIN; charset=US-ASCII
Sender: <franz.com at allegro-cl-request>
Hello!
I am writing a program in LISP, using CLOS. I designed
two independent classes, both with a method called 'add'.
The problem is, the argument list of the two add methods
is different, which is not allowed in CLOS.
Example:
USER(1): (defclass a () ())
#<STANDARD-CLASS A>
USER(2): (defmethod add ((self a) x) (format nil "class a"))
#<STANDARD-METHOD ADD (A T)>
USER(3): (defclass b () ())
#<STANDARD-CLASS B>
USER(4): (defmethod add ((self b) x y) (format nil "class b"))
Error: Attempt to add the method
#<STANDARD-METHOD ADD (B T T) @ #x44c45aa> to the generic
function #<STANDARD-GENERIC-FUNCTION ADD> but the method has
more required arguments than the generic function.
[condition type: PROGRAM-ERROR]
Of course a solution could be to give the methods different
names, but I think that is unsatisfactory.
Why? These are not two different methods of accomplishing the same
thing. They are two different functions that do two different things.
Giving them the same name is confusing. They should get longer,
clearer, names that make the distinction clearer.
I'm guessing that the first function adds its first argument to a collection
denoted by the second argument. What does the third argument to the
second function do? If, for example, it adds the first argument to a collection
denoted by the second argument under a key given by the third
argument, I would call it ADD-WITH-KEY or something similar.
Do you have any ideas of how to get around this? I intend
my program to be platform independent.
You could define the two different classes and generic functions in two
different packages. Then the names of the two generic functions would be
A:ADD, and B:ADD. A package that used both the A and B packages could
specify which of these two symbols should be shadowed, enabling you
to choose on a per-package basis which function would be referred to as
ADD, and which would require package qualification, being referred to as
A:ADD or B:ADD.
Andy Latto
<Pobox.com at andy.latto>