From: Mark  Watson
Subject: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <1146674403.028680.210850@u72g2000cwu.googlegroups.com>
I am probably overlooking something obvious, but how do I access a
shared class variable without creating an instance of the class?

Thanks in advance for any info!

-Mark

From: Mark  Watson
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <1146675278.412239.275210@e56g2000cwe.googlegroups.com>
I am refering to CLOS, BTW.  In:

(defclass SOnode () ;; base class
  ((links           :accessor links   :initarg :links :initform nil)
   (links-meta-data :accessor links-meta-data   :initarg
:links-meta-data :initform nil)
   (hash            :accessor hash :initform (make-hash-table)
:allocation :class)
   (count           :accessor node-count :initform 0 :allocation
:class)))

I want to be able to access the hash and count accessors without having
an instance handy. I could defvar a single instance to use, but I can't
believe that there is not some clean way to access shared variables by
a class reference - I just don't know how to do it.  -thanks.
From: Frode Vatvedt Fjeld
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <2hfyjr6mmu.fsf@vserver.cs.uit.no>
"Mark  Watson" <···········@gmail.com> writes:

> I want to be able to access the hash and count accessors without
> having an instance handy. I could defvar a single instance to use,
> but I can't believe that there is not some clean way to access
> shared variables by a class reference - I just don't know how to do
> it.

One solution is to simply make those class-allocated slots into normal
global variables. I don't think there's much if any semantic
difference between class-allocated slots and global variables anyway,
especially when you need to access the slots outside the scope of a
class instance.

-- 
Frode Vatvedt Fjeld
From: Barry Margolin
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <barmar-4228C3.19533203052006@comcast.dca.giganews.com>
In article <··············@vserver.cs.uit.no>,
 Frode Vatvedt Fjeld <······@cs.uit.no> wrote:

> "Mark  Watson" <···········@gmail.com> writes:
> 
> > I want to be able to access the hash and count accessors without
> > having an instance handy. I could defvar a single instance to use,
> > but I can't believe that there is not some clean way to access
> > shared variables by a class reference - I just don't know how to do
> > it.
> 
> One solution is to simply make those class-allocated slots into normal
> global variables. I don't think there's much if any semantic
> difference between class-allocated slots and global variables anyway,
> especially when you need to access the slots outside the scope of a
> class instance.

The main difference is that a subclass can override a class slot with a 
per-instance slot.

However, assuming you access the slot through reader and writer methods, 
rather than calling SLOT-VALUE directly, you can get the same result by 
having the subclass override the method.

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: Ari Johnson
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <m2bquf2gtv.fsf@hermes.theari.com>
"Mark  Watson" <···········@gmail.com> writes:

> I am probably overlooking something obvious, but how do I access a
> shared class variable without creating an instance of the class?

(defclass my-class () ((count :initform 0 :allocation :class)))
(slot-value (class-prototype (find-class 'my-class)) 'count)

It would be nice if class-prototype were documented in the CLHS.  I
don't see it there.
From: Mark  Watson
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <1146675310.507398.278150@e56g2000cwe.googlegroups.com>
Thanks Ari!
From: William Bland
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <pan.2005.08.30.16.08.09.848886@gmail.com>
On Wed, 03 May 2006 12:50:20 -0400, Ari Johnson wrote:

> "Mark  Watson" <···········@gmail.com> writes:
> 
>> I am probably overlooking something obvious, but how do I access a
>> shared class variable without creating an instance of the class?
> 
> (defclass my-class () ((count :initform 0 :allocation :class)))
> (slot-value (class-prototype (find-class 'my-class)) 'count)
> 
> It would be nice if class-prototype were documented in the CLHS.  I
> don't see it there.

As far as I can see, class-prototype is part of the MOP, which is not part
of ANSI. Hence the CLHS doesn't know about it... incidentally neither does
LispDoc, a flaw which I believe I can fix by indexing the content at
http://www.lisp.org/mop/index.html - added to my TODO list.

Cheers,
	Bill.
From: Ari Johnson
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <m27j532gbd.fsf@hermes.theari.com>
William Bland <···············@gmail.com> writes:

> On Wed, 03 May 2006 12:50:20 -0400, Ari Johnson wrote:
>
>> "Mark  Watson" <···········@gmail.com> writes:
>> 
>>> I am probably overlooking something obvious, but how do I access a
>>> shared class variable without creating an instance of the class?
>> 
>> (defclass my-class () ((count :initform 0 :allocation :class)))
>> (slot-value (class-prototype (find-class 'my-class)) 'count)
>> 
>> It would be nice if class-prototype were documented in the CLHS.  I
>> don't see it there.
>
> As far as I can see, class-prototype is part of the MOP, which is not part
> of ANSI. Hence the CLHS doesn't know about it... incidentally neither does
> LispDoc, a flaw which I believe I can fix by indexing the content at
> http://www.lisp.org/mop/index.html - added to my TODO list.

Is there a way to access that from Slime automagically?  I'd love to
be able to do M-x slime-hyperspec-lookup RET class-prototype RET and
get the documentation.
From: Bill Atkins
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <87u087xbx8.fsf@rpi.edu>
Ari Johnson <················@gmail.com> writes:

> William Bland <···············@gmail.com> writes:
>
>> On Wed, 03 May 2006 12:50:20 -0400, Ari Johnson wrote:
>>
>>> "Mark  Watson" <···········@gmail.com> writes:
>>> 
>>>> I am probably overlooking something obvious, but how do I access a
>>>> shared class variable without creating an instance of the class?
>>> 
>>> (defclass my-class () ((count :initform 0 :allocation :class)))
>>> (slot-value (class-prototype (find-class 'my-class)) 'count)
>>> 
>>> It would be nice if class-prototype were documented in the CLHS.  I
>>> don't see it there.
>>
>> As far as I can see, class-prototype is part of the MOP, which is not part
>> of ANSI. Hence the CLHS doesn't know about it... incidentally neither does
>> LispDoc, a flaw which I believe I can fix by indexing the content at
>> http://www.lisp.org/mop/index.html - added to my TODO list.
>
> Is there a way to access that from Slime automagically?  I'd love to
> be able to do M-x slime-hyperspec-lookup RET class-prototype RET and
> get the documentation.

  M-x common-lisp-hyperspec RET

Better yet:

  (defalias 'clhs 'common-lisp-hyperspec)
  M-x clhs RET

-- 
This is a song that took me ten years to live and two years to write.
 - Bob Dylan
From: Ari Johnson
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <m23bfr2eml.fsf@hermes.theari.com>
Bill Atkins <············@rpi.edu> writes:

>   M-x common-lisp-hyperspec RET
>
> Better yet:
>
>   (defalias 'clhs 'common-lisp-hyperspec)
>   M-x clhs RET

No match for class-prototype.
From: Bill Atkins
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <87mzdzxay9.fsf@rpi.edu>
Ari Johnson <················@gmail.com> writes:

> Bill Atkins <············@rpi.edu> writes:
>
>>   M-x common-lisp-hyperspec RET
>>
>> Better yet:
>>
>>   (defalias 'clhs 'common-lisp-hyperspec)
>>   M-x clhs RET
>
> No match for class-prototype.

Right, CLASS-PROTOTYPE is part of the MOP.  Weren't you asking to
access the CLHS from Emacs?

-- 
This is a song that took me ten years to live and two years to write.
 - Bob Dylan
From: Ari Johnson
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <m2y7xj0zbk.fsf@hermes.theari.com>
Bill Atkins <············@rpi.edu> writes:

> Ari Johnson <················@gmail.com> writes:
>
>> Bill Atkins <············@rpi.edu> writes:
>>
>>>   M-x common-lisp-hyperspec RET
>>>
>>> Better yet:
>>>
>>>   (defalias 'clhs 'common-lisp-hyperspec)
>>>   M-x clhs RET
>>
>> No match for class-prototype.
>
> Right, CLASS-PROTOTYPE is part of the MOP.  Weren't you asking to
> access the CLHS from Emacs?

I quote, with context:

>> As far as I can see, class-prototype is part of the MOP, which is not part
>> of ANSI. Hence the CLHS doesn't know about it... incidentally neither does
>> LispDoc, a flaw which I believe I can fix by indexing the content at
>> http://www.lisp.org/mop/index.html - added to my TODO list.
>
> Is there a way to access that from Slime automagically?  I'd love to
> be able to do M-x slime-hyperspec-lookup RET class-prototype RET and
> get the documentation.

By "that", I meant the MOP documentation.  slime-hyperspec-lookup is
an interactive function provided by Slime.  It works just fine for
CLHS lookups.  What I was asking is if there is a good way to tie the
MOP spec in so that I don't have to always remember when a function is
part of MOP to look it up using the keyboard shortcuts that Slime also
provides by default, such as C-c C-d h
From: Bill Atkins
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <8764knx9lo.fsf@rpi.edu>
Ari Johnson <················@gmail.com> writes:

> Bill Atkins <············@rpi.edu> writes:
>
>> Ari Johnson <················@gmail.com> writes:
>>
>>> Bill Atkins <············@rpi.edu> writes:
>>>
>>>>   M-x common-lisp-hyperspec RET
>>>>
>>>> Better yet:
>>>>
>>>>   (defalias 'clhs 'common-lisp-hyperspec)
>>>>   M-x clhs RET
>>>
>>> No match for class-prototype.
>>
>> Right, CLASS-PROTOTYPE is part of the MOP.  Weren't you asking to
>> access the CLHS from Emacs?
>
> I quote, with context:
>
>>> As far as I can see, class-prototype is part of the MOP, which is not part
>>> of ANSI. Hence the CLHS doesn't know about it... incidentally neither does
>>> LispDoc, a flaw which I believe I can fix by indexing the content at
>>> http://www.lisp.org/mop/index.html - added to my TODO list.
>>
>> Is there a way to access that from Slime automagically?  I'd love to
>> be able to do M-x slime-hyperspec-lookup RET class-prototype RET and
>> get the documentation.
>
> By "that", I meant the MOP documentation.  slime-hyperspec-lookup is
> an interactive function provided by Slime.  It works just fine for
> CLHS lookups.  What I was asking is if there is a good way to tie the
> MOP spec in so that I don't have to always remember when a function is
> part of MOP to look it up using the keyboard shortcuts that Slime also
> provides by default, such as C-c C-d h

Ah, my mistake.

-- 
This is a song that took me ten years to live and two years to write.
 - Bob Dylan
From: Ari Johnson
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <m2mzdz0y10.fsf@hermes.theari.com>
Bill Atkins <············@rpi.edu> writes:
> Ah, my mistake.

Nah.  I'm the one using ambiguous pronouns all over the place.
Although I'd really like to be able to have

(class-prototype 'my-class)
    ^
    `--point is here

and type C-c C-d h and get documentation instead of an error. :)
From: billc
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <1146717102.995392.155660@e56g2000cwe.googlegroups.com>
Hi Ari,

> Nah.  I'm the one using ambiguous pronouns all over the place.
> Although I'd really like to be able to have
>
> (class-prototype 'my-class)
>     ^
>     `--point is here
>
> and type C-c C-d h and get documentation instead of an error. :)

You CAN do exactly that if you use hyperspe-addon.el in addition to
SLIME. I use it and when I press C-c C-d h with the point where you
indicated, I am taken to the relevant section of the online MOP
reference.

I commented on this utility in my blog a couple of years ago:
http://bc.tech.coop/blog/040422.html

--
Bill Clementson
From: Ari Johnson
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <m2fyjq1g2q.fsf@hermes.theari.com>
"billc" <········@gmail.com> writes:

> Hi Ari,
>
>> Nah.  I'm the one using ambiguous pronouns all over the place.
>> Although I'd really like to be able to have
>>
>> (class-prototype 'my-class)
>>     ^
>>     `--point is here
>>
>> and type C-c C-d h and get documentation instead of an error. :)
>
> You CAN do exactly that if you use hyperspe-addon.el in addition to
> SLIME. I use it and when I press C-c C-d h with the point where you
> indicated, I am taken to the relevant section of the online MOP
> reference.
>
> I commented on this utility in my blog a couple of years ago:
> http://bc.tech.coop/blog/040422.html

Bookmarked for my next bout of free time.  Thanks!
From: Pascal Costanza
Subject: Re: Q: accessing shared class variables without creating an instance?
Date: 
Message-ID: <4bs742F13b8k4U1@individual.net>
Ari Johnson wrote:
> "Mark  Watson" <···········@gmail.com> writes:
> 
>> I am probably overlooking something obvious, but how do I access a
>> shared class variable without creating an instance of the class?
> 
> (defclass my-class () ((count :initform 0 :allocation :class)))
> (slot-value (class-prototype (find-class 'my-class)) 'count)
> 
> It would be nice if class-prototype were documented in the CLHS.  I
> don't see it there.

It's not there, it's considered to be part of the CLOS MOP.

You can get an approximation like this:

(defvar *prototypes* (make-hash-table))

(defun class-prototype (class)
   (or (gethash class *prototypes*)
       (setf (gethash class *prototypes*)
             (allocate-instance class))))

By using allocate-instance, you will at least skip the initialization of 
slots (which can be be interesting if, say, the initforms have side 
effects). By using a hashtable, you avoid creating too many prototype 
instances.


Pascal

-- 
3rd European Lisp Workshop
July 3-4 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/