Newsgroups: comp.lang.clos
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!news.mathworks.com!udel!news.sprintlink.net!EU.net!sun4nl!aie.nl!news
From: geert@sun.aie.nl (Geert-Jan van Opdorp)
Subject: Re: how do you define class methods?
Sender: news@aie.nl (News Account)
Organization: AI Engineering BV
Lines: 37
Message-ID: <GEERT.95Jan25095929@sun.aie.nl>
References: <GBOL.95Jan23143247@strident.think.com> <TAR.95Jan23151226@hobbes.ISI.EDU>
In-Reply-To: tar@ISI.EDU's message of 23 Jan 95 15:12:26
Date: Wed, 25 Jan 1995 08:59:28 GMT

In article <TAR.95Jan23151226@hobbes.ISI.EDU> tar@ISI.EDU (Thomas A. Russ) writes:

   From: tar@ISI.EDU (Thomas A. Russ)
   Newsgroups: comp.lang.clos
   Date: 23 Jan 95 15:12:26
   Organization: USC-ISI
   Reply-To: tar@isi.edu

   In article <...> gbol@strident.think.com (Gregory Lampshire) writes:

    > (defclass foo ()
    > 	(a-foo-class-var-counter :allocation :class
    > 				 :accessor counter
    >                                  :initform 0))
    > 
    > 
    > 
    > How does one define a class method to increment the counter without having
    > to reference an instance of the foo class?

   I don't understand your reason for not wanting to reference an instance
   of the class.  Although it is not in standard CLOS, the meta-object
   protocol defines the function class-prototype that returns an instance
   of the class, so you would not need to create an instance of your own
   just to increment the counter.  In this case, you could write:

     (incf (counter (class-prototype (find-class 'foo))))


I can understand this use of an instance, even with 'class-prototype'
feels unsatisfying. It is precisely this kind of problem that makes me 
wonder if not always when ':allocation :class' can be used, it might
be more logical to create a subclass of standard-class containing the
new slot and use that as a metaclass for the new class. Then the class
method asked for is natural, and if necesary the slot can be accessed
from an instance by means of '(counter (class-of <instance>))'

