:ALLOCATION :CLASS: getting slot value 
Author Message
 :ALLOCATION :CLASS: getting slot value

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



Sun, 11 Sep 2005 22:36:40 GMT  
 :ALLOCATION :CLASS: getting slot value

Quote:
> 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)))



Sun, 11 Sep 2005 22:58:07 GMT  
 :ALLOCATION :CLASS: getting slot value
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

Quote:

> 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



Sun, 11 Sep 2005 23:03:49 GMT  
 :ALLOCATION :CLASS: getting slot value

Quote:

> * On the subject of ":ALLOCATION :CLASS: getting slot value"
> * Sent on Wed, 26 Mar 2003 16:36:40 +0200

> (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.



Sun, 11 Sep 2005 23:29:23 GMT  
 :ALLOCATION :CLASS: getting slot value
... 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; }

Quote:
}

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".

Quote:

> 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



Mon, 12 Sep 2005 00:12:37 GMT  
 :ALLOCATION :CLASS: getting slot value

Quote:

> [..] 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



Mon, 12 Sep 2005 01:01:23 GMT  
 :ALLOCATION :CLASS: getting slot value
Hello,

Quote:

> ... 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."



Mon, 12 Sep 2005 01:59:49 GMT  
 :ALLOCATION :CLASS: getting slot value

Quote:

> Hello,


> > ... 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



Mon, 12 Sep 2005 02:16:06 GMT  
 :ALLOCATION :CLASS: getting slot value


Quote:
>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))

--

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.



Mon, 12 Sep 2005 02:34:15 GMT  
 :ALLOCATION :CLASS: getting slot value

Quote:

> In article ,

> >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



Mon, 12 Sep 2005 05:46:19 GMT  
 :ALLOCATION :CLASS: getting slot value


Quote:
> > 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



Mon, 12 Sep 2005 07:02:28 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. CLOS: slots with :allocation :class

2. accessibility :allocation :class slot

3. Getting the slots of a class

4. Dylan Tip #101: don't use keyword init-values with class allocated slots

5. Back door to 'slot-value-using-class?

6. Problem with allocation keywords in slot-specs

7. Fix for shared slot allocation

8. CLOS :allocation slot types

9. Getting shared slot without instance

10. Limiting accepted values in slots

11. Objects with Lazy Evaluation Slot Values

12. incrementing slot value

 

 
Powered by phpBB® Forum Software