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 ?
--
Vladimir Zolotykh ······@eurocom.od.ua
From: ·······@andrew.cmu.edu
Subject: Re: incrementing slot value
Date:
Message-ID: <20020121122609.J8529@emu>
On Mon, Jan 21, 2002 at 04:22:06PM +0200, Vladimir Zolotykh wrote:
> 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.
--
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Matthew Danish email: ·······@andrew.cmu.edu ;;
;; OpenPGP public key available from: 'finger ···@db.debian.org' ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Vladimir Zolotykh wrote:
>
> 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.
Vladimir Zolotykh wrote:
>
> 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.