incrementing slot value 
Author Message
 incrementing slot value

Consider the following

  (defclass acct ()
    ((sum :initarg sum :initform 0 :reader acct-sum)))

  (defmethod (setf acct-sum) (new (a acct))
    (setf (slot-value a 'sum) new))

Now I'd like to increment slot SUM by some number. How I can do that ?

#1

  (defmethod acct-sum-incf ((a acct) &optional (delta 1))
    (setf (slot-value a 'sum) (+ (slot-value a 'sum) delta)))

Here no doubts, I think.

#2

  (defmethod acct-sum-incf-2 ((a acct) &optional (delta 1))
    (incf (slot-value a 'sum) delta))

This one less obvious to me. Should it always work ?

#3

  (incf (acct-sum *a*) delta))

Where *a* is an instance of ACCT. About this I aslo couldn't say
much. Is it reliable if possible at all ?

--



Fri, 09 Jul 2004 22:22:06 GMT  
 incrementing slot value

Quote:

> Consider the following

>   (defclass acct ()
>     ((sum :initarg sum :initform 0 :reader acct-sum)))

>   (defmethod (setf acct-sum) (new (a acct))
>     (setf (slot-value a 'sum) new))

> Now I'd like to increment slot SUM by some number. How I can do that ?

> #1

>   (defmethod acct-sum-incf ((a acct) &optional (delta 1))
>     (setf (slot-value a 'sum) (+ (slot-value a 'sum) delta)))

> Here no doubts, I think.

> #2

>   (defmethod acct-sum-incf-2 ((a acct) &optional (delta 1))
>     (incf (slot-value a 'sum) delta))

> This one less obvious to me. Should it always work ?

> #3

>   (incf (acct-sum *a*) delta))

> Where *a* is an instance of ACCT. About this I aslo couldn't say
> much. Is it reliable if possible at all ?

Your confusion seems mostly to be about incf.  incf is a
macro.  To understand it, you should macroexpand it.  If your
implementation provides no better tools, try executing this:

 (macroexpand '(incf (acct-sum *a*) delta))

and then compare your three code samples to understand how they
are similar and how they are different.



Sat, 10 Jul 2004 00:19:26 GMT  
 incrementing slot value

Quote:

> Consider the following

>   (defclass acct ()
>     ((sum :initarg sum :initform 0 :reader acct-sum)))

>   (defmethod (setf acct-sum) (new (a acct))
>     (setf (slot-value a 'sum) new))

> Now I'd like to increment slot SUM by some number. How I can do that ?

> #1

>   (defmethod acct-sum-incf ((a acct) &optional (delta 1))
>     (setf (slot-value a 'sum) (+ (slot-value a 'sum) delta)))

> Here no doubts, I think.

> #2

>   (defmethod acct-sum-incf-2 ((a acct) &optional (delta 1))
>     (incf (slot-value a 'sum) delta))

> This one less obvious to me. Should it always work ?

> #3

>   (incf (acct-sum *a*) delta))

> Where *a* is an instance of ACCT. About this I aslo couldn't say
> much. Is it reliable if possible at all ?

Your confusion seems mostly to be about incf.  incf is a
macro.  To understand it, you should macroexpand it.  If your
implementation provides no better tools, try executing this:

 (macroexpand '(incf (acct-sum *a*) delta))

and then compare your three code samples to understand how they
are similar and how they are different.



Sat, 10 Jul 2004 00:19:06 GMT  
 incrementing slot value

Quote:

> Consider the following

>   (defclass acct ()
>     ((sum :initarg sum :initform 0 :reader acct-sum)))

>   (defmethod (setf acct-sum) (new (a acct))
>     (setf (slot-value a 'sum) new))

> #3

>   (incf (acct-sum *a*) delta))

> Where *a* is an instance of ACCT. About this I aslo couldn't say
> much. Is it reliable if possible at all ?

Yes, since you've defined a setf-method for acct-sum.  You could have
also achieved this by using :accessor instead of :reader for acct-sum.

--
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



Sat, 10 Jul 2004 01:26:09 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. How can I Increment values logarithmically?

2. auto increment value starting from n

3. Re : incrementing values

4. incrementing values

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

6. Limiting accepted values in slots

7. Objects with Lazy Evaluation Slot Values

8. :ALLOCATION :CLASS: getting slot value

9. :initform and slot-value

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

11. Accessing foreign type array slot value (ACL 5.0)

12. with-slots and multiple-value-bind

 

 
Powered by phpBB® Forum Software