konstan@elmer-fudd.berkeley.edu (Joe Konstan) (04/03/91)
Is there an easy way to accomplish the following:
(Assume I have a typical inheritance tree as follows:
A
/ \
Aa Ab etc.
/ \ \
Aa1 Aa2 Ab1
I want to define a slot named "default" that is a class slot for all of these
classes but has a separate value in each class. In other words, I want
(defclass A ()
((default :allocation :class :initform 10 ...)
...))
but also want each subclass to have its own slot named default that can
be changed separately.
I know this can be done by adding a new slot definition in the defclass for
each subclass, but this is undesirable (for several reasons). Is there an
alternative?
Joe KonstanGregor@parc.xerox.com (Gregor Kiczales) (04/03/91)
Organization: Picasso Research Group, UC Berkeley CS Division
Date: Tue, 2 Apr 1991 15:07:05 PST
From: konstan@elmer-fudd.berkeley.edu (Joe Konstan)
I want to define a slot named "default" that is a class slot for all
of these classes but has a separate value in each class. In other
words, I want
(defclass A ()
((default :allocation :class :initform 10 ...)
...))
but also want each subclass to have its own slot named default that
can be changed separately.
It's funny you should bring this up just now. I wanted something
similar myself earlier today. I can think of two ways to do this.
1) With a special macro, like DEFINE-FROBBOZ, that takes the same
arguments as DEFCLASS and expands to a DEFCLASS with the additional
slot. Given this macro, you would write:
(define-frobboz Aa1 (Aa) ())
Which saves the extra slot from appearing throughout your code.
2) With a special class metaobject class (metaclass) that simply adds
the extra class slot as a direct slot in each of its instances. In the
final metaobject protocol, this would be written as:
(defclass frobboz-class (standard-class) ())
(defmethod initialize-instance ((class frobboz-class) &key direct-slots
&rest keys)
(apply #'call-next-method
:direct-slots (cons '(:name default :allocation :class)
direct-slots)
keys))
Depending on what version of PCL you are using, you will have to adjust
this a bit. Given this, you would write:
(defclass Aa1 (Aa) () (:metaclass frobboz-class))
Which also saves the extra slot from appearing throughout your code, in
a slightly different way.
If all you want to associate with the class is the extra slot, the first
solution is probably better. In the case I was working with earlier
today, I am likely to choose the second solution because there is more I
want to associate with the class.