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

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

> much. Is it reliable if possible at all ?

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

> much. Is it reliable if possible at all ?

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

> much. Is it reliable if possible at all ?

Yes, since you've defined a setf-method for acct-sum.  You could have

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

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

Sat, 10 Jul 2004 01:26:09 GMT

 Page 1 of 1 [ 4 post ]

Relevant Pages