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?
Thanks.
Blake McBride
On Dec 31, 5:49 pm, Blake McBride <·····@mcbride.name> 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?
Why do you say that #2 is "theoretically pure" (whatever that means)?
A problem of #2 is that meta classes don't compose, so I'd choose #1
unless there are specific reasons speaking against it. Then again,
there's nothing wrong with specials either.
Cheers,
Michael
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/