From: ········@ptcstudios.com
Subject: CLOS: slots with :allocation :class
Date: 
Message-ID: <HsRwOSVR3HtSUeO8MaRa=8tsr7af@4ax.com>
Hi-

I understand that in CLOS slots can be given an :allocation :class.
This makes the slot shared by all objects in a class.  I am wondering
if this slot can be accessed before an object of the class is
intantiated.

I presume that this value is actually stored in the class metaobjet,
but I cannot find where.  I reviewed the CLOS and MOP specifications
and couldn't find this.  Any help would be very appreicated.

Thanks,
········@ptcstudios.com

From: Frode Vatvedt Fjeld
Subject: Re: CLOS: slots with :allocation :class
Date: 
Message-ID: <2hog3z6lv8.fsf@dslab7.cs.uit.no>
········@ptcstudios.com writes:

> I understand that in CLOS slots can be given an :allocation :class.
> This makes the slot shared by all objects in a class.  I am
> wondering if this slot can be accessed before an object of the class
> is intantiated.

In general, it can't. But after some thinking you'll realize that
class-allocated slots are almost equivalent to global
variables. "Almost" because you don't get the class dispatcing (which
you'll only need if your program must access the class-allocated slot
of some unknown class), but that's easy to add manually.

(defclass my-class ()
  ((regular-slot :accessor my-class-regular)))

(defvar *my-class-ca-slot* 42)

..and of course you access my-class's "class-allocated" slot by
accessing the variable my-class-class-allocated-slot. And, if you need
it, or just want consistency in the syntax for slot access:

;; access by class object
(defmethod my-class-ca ((class (eql (find-class 'my-class))))
  *my-class-ca-slot*)

(defmethod (setf my-class-ca) (value (class (eql (find-class 'c))))
  (setf *my-class-ca-slot* value))

;; you may also want to include access by class-name..

;; access by instance
(defmethod my-class-ca ((instance my-class))
  *my-class-ca-slot*)

(defmethod (setf my-class-ca) (value (instance my-class))
  (setf *my-class-ca-slot* value))


Now you may access your class-allocated slot with

    (my-class-ca (find-class 'my-class))

If you find all this too clumsy, you can embed it in a macro (the
definition of macro DEF-CLASS-ALLOCATED-SLOT is left as an exercise
for the reader), or you can probably integrate the concept seamlessly
into CLOS by means of the MOP.

-- 
Frode Vatvedt Fjeld
From: Frode Vatvedt Fjeld
Subject: Re: CLOS: slots with :allocation :class
Date: 
Message-ID: <2hk8en6lr2.fsf@dslab7.cs.uit.no>
········@ptcstudios.com writes:

> I understand that in CLOS slots can be given an :allocation :class.
> This makes the slot shared by all objects in a class.  I am
> wondering if this slot can be accessed before an object of the class
> is intantiated.

In general, it can't. But after some thinking you'll realize that
class-allocated slots are almost equivalent to global
variables. "Almost" because you don't get the class dispatcing (which
you'll only need if your program must access the class-allocated slot
of some unknown class), but that's easy to add manually.

(defclass my-class ()
  ((regular-slot :accessor my-class-regular)))

(defvar *my-class-ca-slot* 42)

..and of course you access my-class's "class-allocated" slot by
accessing the variable my-class-class-allocated-slot. And, if you need
it, or just want consistency in the syntax for slot access:

;; access by class object
(defmethod my-class-ca ((class (eql (find-class 'my-class))))
  *my-class-ca-slot*)

(defmethod (setf my-class-ca) (value (class (eql (find-class 'my-class))))
  (setf *my-class-ca-slot* value))

;; you may also want to include access by class-name..

;; access by instance
(defmethod my-class-ca ((instance my-class))
  *my-class-ca-slot*)

(defmethod (setf my-class-ca) (value (instance my-class))
  (setf *my-class-ca-slot* value))


Now you may access your class-allocated slot with

    (my-class-ca (find-class 'my-class))

If you find all this too clumsy, you can embed it in a macro (the
definition of macro DEF-CLASS-ALLOCATED-SLOT is left as an exercise
for the reader), or you can probably integrate the concept seamlessly
into CLOS by means of the MOP.

-- 
Frode Vatvedt Fjeld
From: David Bakhash
Subject: Re: CLOS: slots with :allocation :class
Date: 
Message-ID: <c297lal4zmj.fsf@nerd-xing.mit.edu>
········@ptcstudios.com writes:

> I understand that in CLOS slots can be given an :allocation :class.
> This makes the slot shared by all objects in a class.  I am wondering
> if this slot can be accessed before an object of the class is
> intantiated.
> 
> I presume that this value is actually stored in the class metaobjet,
> but I cannot find where.  I reviewed the CLOS and MOP specifications
> and couldn't find this.  Any help would be very appreicated.

Thinking about your question, I can't really see the point.  Consider
that a reader, or accessor function for that slot MUST operate on an
instance of that class, the answer seems to be "no".  I am sure that the 
the implementation there is a way to get at that value given only the
class.  Maybe:

(slot-value (make-instance '<the-class>) '<the-slot>)

but this is still no good, since you don't know what the side effects of 
instantiating an object of that class are.

Slots, regardless of allocation, belong to instances.  Keeping this in
mind should answer your question, though admittedly I don't know the
answer for sure.  

dave
From: Francis Leboutte
Subject: Re: CLOS: slots with :allocation :class
Date: 
Message-ID: <419rnsgqqasc7lknkc5ntds7as91vll30f@4ax.com>
David Bakhash <·····@alum.mit.edu> wrote:

>········@ptcstudios.com writes:
>
>> I understand that in CLOS slots can be given an :allocation :class.
>> This makes the slot shared by all objects in a class.  I am wondering
>> if this slot can be accessed before an object of the class is
>> intantiated.
>> [...]
>
>Thinking about your question, I can't really see the point.  Consider
>that a reader, or accessor function for that slot MUST operate on an
>instance of that class, the answer seems to be "no".  I am sure that the 
> [...]


From the quite recent `Getting shared slot without instance' thread:

>Tim Bradshaw <···@cley.com> wrote:
>
>>* I wrote:
>>
>>> If you are happy to use a MOP, and if you have a Lisp that is close to
>>> AMOP, then you can probbably use something based around
>>> CLASS-PROTOTYPE, to get a prototype instance.
>>
>>I meant to attach what I use, but I couldn't find it.  This is
>>basically it.  I'd like to know if anyone knows whether this will
>>actually work if no instances have been made (is the prototype
>>initialised in that case?):
>>
>>
>>    (in-package :cl-user)
>>    #+allegro
>>    (use-package :clos)
>>
>>    (defgeneric class-slot-value (class slot-name))
>>
>>    (defmethod class-slot-value ((class standard-class) slot-name)
>>      (unless (class-finalized-p class)
>>	(finalize-inheritance class))
>>      (slot-value (class-prototype class) slot-name))
>>
>>    (defmethod class-slot-value ((class symbol) slot-name)
>>      (class-slot-value (find-class class) slot-name))
>>
>
>I use something similar with ACL (windows). This works fine even if there is no instances (for persistent classes too - Allegrostore) .
>
>Same thing to set the value of such a slot  :
>
>(defmethod (setf class-slot-value) (new (class symbol) slot-name)
>  (setf (class-slot-value (find-class class) slot-name)
>    new))
>
>(defmethod (setf class-slot-value) (new (class standard-class) slot-name)
>  (unless (class-finalized-p class)
>    (finalize-inheritance class))
>  (setf (slot-value (class-prototype class) slot-name)
>    new))
>
>F.

--
Francis Leboutte
··@algo.be   www.algo.be   +32-(0)4.388.39.19