Re: Class slots in CLOS
- From: Pascal Costanza <pc@xxxxxxxxx>
- Date: Mon, 31 Dec 2007 19:59:14 +0100
Blake McBride wrote:
Greetings,
It seems to me that there are two ways to define class slots (slots common to all instances of the class) in CLOS as follows:
1. via the use of :allocation :class slot option
2. By the creation and use of a meta-class that is to house the shared slots for a given class or set of classes.
I suppose my main question is what is the difference. I'm pretty sure method #1 is easier to declare and access to the class slots is easier (just like instance slots). Is one faster than the other.
I understand that using method #2 is more flexible and theoretically pure. Or, is method #1 just a shorthand for method #2?
No, method #2 is not more flexible. There is an important technical difference, which also implies a semantical difference.
The technical difference is that with method #1, you need an instance of a class to access the class slot, while with method #2, the slots are actually not class slots, but instance slots of the class metaobject, so you need the class metaobject to access the slots.
In code:
[1]
(defclass foo ()
((foo-slot :allocation :class :initform 0 :reader foo-slot)))
(defvar *foo* (make-instance 'foo))
(foo-slot *foo*)
=> yields class slot 'foo-slot of class foo.
[2]
(defclass foo-class ()
((foo-slot :initform 0 :reader foo-slot)))
(defclass foo-object ()
()
(:metaclass foo-class))
(defvar *foo* (make-instance 'foo-object))
(foo-slot (class-of *foo*))
=> yields instance slot 'foo-slot of the foo-class metaobject
Among other things, there is a well-defined inheritance and overriding relationship for class slots, including the interactions between class slots and instance slots, while for method [2], there is no inheritance at all between the slots of the class metaobjects in a class hierarchy.
Pascal
--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
.
- References:
- Class slots in CLOS
- From: Blake McBride
- Class slots in CLOS
- Prev by Date: Re: clsql hunchentoot problem working together
- Next by Date: Re: Curses alternative for Lisp?
- Previous by thread: Re: Class slots in CLOS
- Index(es):