From: Vladimir Zolotykh
Subject: :ALLOCATION :CLASS: getting slot value
Date: 
Message-ID: <3E81BAF8.4030608@eurocom.od.ua>
Hi,

Here is a very simple example, with the intention to count the number of
instantiated objects of the particular type

(defclass self-counter ()
   ((how-many :allocation :class :initform 0
              :reader self-counter-value)))

(defmethod initialize-instance :after ((obj self-counter) &rest args)
   (incf (slot-value obj 'how-many)))

(let ((obj (make-instance 'self-counter)))
   (print (self-counter-value obj))
   (make-instance 'self-counter)
   (print (self-counter-value obj)))

This probable achieves its purpose with the one small exception. I
don't know how to obtain the value of HOW-MANY slot without object of
type 'SELF-COUNTER at hand. Is it possible? If so would you tell me
how?

Thanks in advance

-- 
Vladimir Zolotykh

From: Marco Antoniotti
Subject: :ALLOCATION :CLASS: setting slot value (Re: :ALLOCATION :CLASS: getting slot value)
Date: 
Message-ID: <Wjkga.120$oj7.12279@typhoon.nyu.edu>
... on a related matter

I know this may be a FAQ, however, I stumbled upon the following idiom 
in C++

class foo {
	static int bar;
public:
	static void InitFoo(int x) { bar = x; }
}


I promptly translated this as

(defclass foo () ((bar :allocation :class :accessor foo-bar)))

Now, to set the slot value I did

	(setf (foo-bar (make-instance 'foo)) 42)

Is there a better way that is escaping me?

Cheers

Marco


PS.  Note that I cannot do this at initialization time. Somehow the C++ 
idiom "feels right".




Vladimir Zolotykh wrote:

> Hi,
>
> Here is a very simple example, with the intention to count the number of
> instantiated objects of the particular type
>
> (defclass self-counter ()
>   ((how-many :allocation :class :initform 0
>              :reader self-counter-value)))
>
> (defmethod initialize-instance :after ((obj self-counter) &rest args)
>   (incf (slot-value obj 'how-many)))
>
> (let ((obj (make-instance 'self-counter)))
>   (print (self-counter-value obj))
>   (make-instance 'self-counter)
>   (print (self-counter-value obj)))
>
> This probable achieves its purpose with the one small exception. I
> don't know how to obtain the value of HOW-MANY slot without object of
> type 'SELF-COUNTER at hand. Is it possible? If so would you tell me
> how?
>
> Thanks in advance
>
From: Jorge Tavares
Subject: Re: :ALLOCATION :CLASS: setting slot value (Re: :ALLOCATION :CLASS: getting  slot value)
Date: 
Message-ID: <3E81EA95.9040706@dei.uc.pt>
Hello,

Marco Antoniotti wrote:
> ... on a related matter
> 
> I know this may be a FAQ, however, I stumbled upon the following idiom 
> in C++
> 
> class foo {
>     static int bar;
> public:
>     static void InitFoo(int x) { bar = x; }
> }
> 
> 
> I promptly translated this as
> 
> (defclass foo () ((bar :allocation :class :accessor foo-bar)))
> 
> Now, to set the slot value I did
> 
>     (setf (foo-bar (make-instance 'foo)) 42)
> 
> Is there a better way that is escaping me?

Well, you can add an :initarg to your class definition:

(defclass foo ()
   ((bar :allocation :class
         :accessor foo-bar
         :initarg :bar)))

this way you can pass the value you want to the slot when creating an 
object, like:

(setf foo (make-instance 'foo :bar 42))


or, if know from the begining what the value you want in the slot, you 
can use :initform instead:

(defclass foo ()
   ((bar :allocation :class
         :accessor foo-bar
         :initform 42)))

And then, you only need to creat an object, and the slot will be filled:

(setf foo (make-instance 'foo))


If I understood right, I think this is what you wanted.

Best regards,
Jorge

-- 
Jorge Tavares
http://www.dei.uc.pt/~jast

"Evolution is the best engineer."
From: Marco Antoniotti
Subject: Re: :ALLOCATION :CLASS: setting slot value (Re: :ALLOCATION :CLASS: getting  slot value)
Date: 
Message-ID: <H7mga.124$oj7.12210@typhoon.nyu.edu>
Jorge Tavares wrote:

> Hello,
>
> Marco Antoniotti wrote:
>
> > ... on a related matter
> >
> > I know this may be a FAQ, however, I stumbled upon the following idiom
> > in C++
> >
> > class foo {
> >     static int bar;
> > public:
> >     static void InitFoo(int x) { bar = x; }
> > }
> >
> >
> > I promptly translated this as
> >
> > (defclass foo () ((bar :allocation :class :accessor foo-bar)))
> >
> > Now, to set the slot value I did
> >
> >     (setf (foo-bar (make-instance 'foo)) 42)
> >
> > Is there a better way that is escaping me?
>
>
> Well, you can add an :initarg to your class definition:
>
> (defclass foo ()
>   ((bar :allocation :class
>         :accessor foo-bar
>         :initarg :bar)))
>
> this way you can pass the value you want to the slot when creating an
> object, like:
>
> (setf foo (make-instance 'foo :bar 42))
>
>
> or, if know from the begining what the value you want in the slot, you
> can use :initform instead:
>
> (defclass foo ()
>   ((bar :allocation :class
>         :accessor foo-bar
>         :initform 42)))
>
> And then, you only need to creat an object, and the slot will be filled:
>
> (setf foo (make-instance 'foo))
>
>
> If I understood right, I think this is what you wanted.


No.  Specifically, I *do not* want an :initarg on the shared slot, nor I 
want an :initform.

Cheers

--
Marco Antoniotti
From: Barry Margolin
Subject: Re: :ALLOCATION :CLASS: setting slot value (Re: :ALLOCATION :CLASS: getting  slot value)
Date: 
Message-ID: <Homga.10$Hv4.221@paloalto-snr1.gtei.net>
In article <···················@typhoon.nyu.edu>,
Marco Antoniotti  <·······@cs.nyu.edu> wrote:
>No.  Specifically, I *do not* want an :initarg on the shared slot, nor I 
>want an :initform.

If you want an init-foo function, analogous to the C++ example you gave,
you can do something like:

(defun init-foo (x)
  (setf (foo-bar (mop:class-prototype 'foo)) x))

-- 
Barry Margolin, ··············@level3.com
Genuity Managed Services, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Marco Antoniotti
Subject: Re: :ALLOCATION :CLASS: setting slot value (Re: :ALLOCATION :CLASS: getting  slot value)
Date: 
Message-ID: <Lcpga.125$oj7.12427@typhoon.nyu.edu>
Barry Margolin wrote:

> In article ,
> Marco Antoniotti   wrote:
>
> >No.  Specifically, I *do not* want an :initarg on the shared slot, nor I
> >want an :initform.
>
>
> If you want an init-foo function, analogous to the C++ example you gave,
> you can do something like:
>
> (defun init-foo (x)
>   (setf (foo-bar (mop:class-prototype 'foo)) x))
>
That is essentially what I am doing.  I just use a throw away 
MAKE-INSTANCE to achieve that.  I assumed that some MOPpery was 
involved; alas, MOP is not standard.

Maybe

	(setf (foo-bar (allocate-instance 'foo)) 42)

is enough?

Cheers

--
Marco Antoniotti
From: Pascal Costanza
Subject: Re: :ALLOCATION :CLASS: setting slot value (Re: :ALLOCATION :CLASS: getting  slot value)
Date: 
Message-ID: <costanza-E7BB3C.00022827032003@news.netcologne.de>
In article <···················@typhoon.nyu.edu>,
 Marco Antoniotti <·······@cs.nyu.edu> wrote:

> > If you want an init-foo function, analogous to the C++ example you gave,
> > you can do something like:
> >
> > (defun init-foo (x)
> >   (setf (foo-bar (mop:class-prototype 'foo)) x))
> >
> That is essentially what I am doing.  I just use a throw away 
> MAKE-INSTANCE to achieve that.  I assumed that some MOPpery was 
> involved; alas, MOP is not standard.
> 
> Maybe
> 
> 	(setf (foo-bar (allocate-instance 'foo)) 42)
> 
> is enough?

Someone suggested so in another discussion (I guess it was in the MCL 
mailing list).

However, class-prototype seems to me to be The Right Thing (tm) in this 
case. So if your CL implementation doesn't provide it, I'd suggest to 
write your own class-prototype macro/function that expands into/calls 
allocate-instance, and you should be on the safe side IMHO.


Pascal

-- 
"If I could explain it, I wouldn't be able to do it."
A.M.McKenzie
From: Clive Tong
Subject: Re: :ALLOCATION :CLASS: getting slot value
Date: 
Message-ID: <u1y0ucgzk.fsf@scientia.com>
> This probable achieves its purpose with the one small exception. I
> don't know how to obtain the value of HOW-MANY slot without object of
> type 'SELF-COUNTER at hand. Is it possible? If so would you tell me
> how?

class-prototype might be what you're looking for.

(self-counter-value (class-prototype (find-class 'self-counter)))
From: Kenny Tilton
Subject: Re: :ALLOCATION :CLASS: getting slot value
Date: 
Message-ID: <3E81C2EB.9000400@nyc.rr.com>
Let's get this one in the FAQ! <g>:

(let ((c (find-class 'self-counter)))
   (mop:finalize-inheritance c)
   (self-counter-value (mop:class-prototype c)))

That's in ACL. I think I did similar in MCL. Others, I do not know about.

kt

Vladimir Zolotykh wrote:
> Hi,
> 
> Here is a very simple example, with the intention to count the number of
> instantiated objects of the particular type
> 
> (defclass self-counter ()
>   ((how-many :allocation :class :initform 0
>              :reader self-counter-value)))
> 
> (defmethod initialize-instance :after ((obj self-counter) &rest args)
>   (incf (slot-value obj 'how-many)))
> 
> (let ((obj (make-instance 'self-counter)))
>   (print (self-counter-value obj))
>   (make-instance 'self-counter)
>   (print (self-counter-value obj)))
> 
> This probable achieves its purpose with the one small exception. I
> don't know how to obtain the value of HOW-MANY slot without object of
> type 'SELF-COUNTER at hand. Is it possible? If so would you tell me
> how?
> 
> Thanks in advance
> 


-- 

  kenny tilton
  clinisys, inc
  http://www.tilton-technology.com/
  ---------------------------------------------------------------
"Cells let us walk, talk, think, make love and realize
  the bath water is cold." -- Lorraine Lee Cudmore
From: Sam Steingold
Subject: Re: :ALLOCATION :CLASS: getting slot value
Date: 
Message-ID: <m3u1dqxi02.fsf@loiso.podval.org>
> * In message <················@eurocom.od.ua>
> * On the subject of ":ALLOCATION :CLASS: getting slot value"
> * Sent on Wed, 26 Mar 2003 16:36:40 +0200
> * Honorable Vladimir Zolotykh <······@eurocom.od.ua> writes:
>
> (defclass self-counter ()
>    ((how-many :allocation :class :initform 0
>               :reader self-counter-value)))
> 
> This probable achieves its purpose with the one small exception. I
> don't know how to obtain the value of HOW-MANY slot without object of
> type 'SELF-COUNTER at hand. Is it possible? If so would you tell me
> how?

MOP:

                Generic Function class-prototype class

        Returns a prototype instance of class. Whether the instance is
        initialized is not specified. The results are undefined if a
        portable program modifies the binding of any slot of prototype
        instance.

        This generic function signals an error if class has not been
        finalized.

CLISP does not support MOP, so you need to use ad hoc code, see
<http://article.gmane.org/gmane.lisp.clisp.general/1491/>
and the concomitant discussion thread.

-- 
Sam Steingold (http://www.podval.org/~sds) running RedHat8 GNU/Linux
<http://www.camera.org> <http://www.iris.org.il> <http://www.memri.org/>
<http://www.mideasttruth.com/> <http://www.palestine-central.com/links.html>
He who laughs last did not get the joke.
From: Frode Vatvedt Fjeld
Subject: Re: :ALLOCATION :CLASS: getting slot value
Date: 
Message-ID: <2hisu66p0c.fsf@vserver.cs.uit.no>
Vladimir Zolotykh <······@eurocom.od.ua> writes:

> [..] This probable achieves its purpose with the one small
> exception. I don't know how to obtain the value of HOW-MANY slot
> without object of type 'SELF-COUNTER at hand.

Consider if what you're doing is better done with a global variable
than a class-allocated slot.

-- 
Frode Vatvedt Fjeld
From: Steven M. Haflich
Subject: Re: :ALLOCATION :CLASS: getting slot value
Date: 
Message-ID: <3E829356.9070206@alum.mit.edu>
Frode Vatvedt Fjeld wrote:

> Consider if what you're doing is better done with a global variable
> than a class-allocated slot.

It is not better done with a global variable.  Consider the options
if multiple subclass-related classes directly specify this slot.
From: Frode Vatvedt Fjeld
Subject: Re: :ALLOCATION :CLASS: getting slot value
Date: 
Message-ID: <2hel4t6p0l.fsf@vserver.cs.uit.no>
> Frode Vatvedt Fjeld wrote:
>
>> Consider if what you're doing is better done with a global variable
>> than a class-allocated slot.

"Steven M. Haflich" <·················@alum.mit.edu> writes:

> It is not better done with a global variable.  Consider the options
> if multiple subclass-related classes directly specify this slot.

I wasn't saying that global variables are equivalent to
class-allocated slots, but that they are similar, and even more so
when you find that you need to get at the value without having an
instance at hand. And that I've found several times that what I first
think should be a class-allocated slot worked better as a global
variable. Also, there's a middle ground that looks something like
this:

  (defvar *my-class-x* 'foo)
  (defmethod my-class-x ((object my-class)) *my-class-x*)
  (defmethod (setf my-class-x) ...)

and so on, with a different variable and methods for subclasses as
appropriate. Or, in the case of read-only class-allocated slots, you
don't even need the variable:

  (defmethod my-class-x ((object my-class)) 'foo)

-- 
Frode Vatvedt Fjeld