Article View: comp.lang.clos
Article #476Re: How to get class slots with an instance at each class
From: Gregor Kiczales
Date: Wed, 03 Apr 1991 02:10
Date: Wed, 03 Apr 1991 02:10
53 lines
2532 bytes
2532 bytes
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.
Technical Details
Message-ID:
<91Apr2.181018pst.29186@tracer-bullet.parc.xerox.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uwm.edu!bionet!agate!ucbvax!tut.cis.ohio-state.edu!unreplyable!garbageReferences:
<41558@ucbvax.BERKELEY.EDU>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!uwm.edu!bionet!agate!ucbvax!tut.cis.ohio-state.edu!unreplyable!garbage From: Gregor@parc.xerox.com (Gregor Kiczales) Newsgroups: comp.lang.clos Subject: Re: How to get class slots with an instance at each class Message-ID: <91Apr2.181018pst.29186@tracer-bullet.parc.xerox.com> Date: 3 Apr 91 02:10:10 GMT References: <41558@ucbvax.BERKELEY.EDU> Sender: welch@tut.cis.ohio-state.edu Distribution: inet Organization: CommonLoops Lines: 53