Skip to main content

with-accessors

with-accessors Macro

Syntax:

with-accessors ({slot-entry}*) instance-form {declaration}* {form}*

→ {result}*

slot-entry::=(variable-name accessor-name)

Arguments and Values:

variable-name—a variable name; not evaluated.

accessor-name—a function name; not evaluated.

instance-form—a form; evaluated.

declaration—a declare expression; not evaluated.

forms—an implicit progn.

results—the values returned by the forms.

Description:

Creates a lexical environment in which the slots specified by slot-entry are lexically available through their accessors as if they were variables. The macro with-accessors invokes the appropriate accessors to access the slots specified by slot-entry. Both setf and setq can be used to set the value of the slot.

with-accessors

Examples:

(defclass thing () 
((x :initarg :x :accessor thing-x)
(y :initarg :y :accessor thing-y)))
→ #<STANDARD-CLASS THING 250020173>
(defmethod (setf thing-x) :before (new-x (thing thing))
(format t "~&Changing X from ~D to ~D in ~S.~%"
(thing-x thing) new-x thing))
(setq thing1 (make-instance ’thing :x 1 :y 2)) → #<THING 43135676>
(setq thing2 (make-instance ’thing :x 7 :y 8)) → #<THING 43147374>
(with-accessors ((x1 thing-x) (y1 thing-y))
thing1
(with-accessors ((x2 thing-x) (y2 thing-y))
thing2
(list (list x1 (thing-x thing1) y1 (thing-y thing1)
x2 (thing-x thing2) y2 (thing-y thing2))
(setq x1 (+ y1 x2))
(list x1 (thing-x thing1) y1 (thing-y thing1)
x2 (thing-x thing2) y2 (thing-y thing2))
(setf (thing-x thing2) (list x1))
(list x1 (thing-x thing1) y1 (thing-y thing1)
x2 (thing-x thing2) y2 (thing-y thing2)))))
▷ Changing X from 1 to 9 in #<THING 43135676>.
▷ Changing X from 7 to (9) in #<THING 43147374>.
((1 1 2 2 7 7 8 8)
9
(9 9 2 2 7 7 8 8)
(9)
(9 9 2 2 (9) (9) 8 8))

Affected By:

defclass

Exceptional Situations:

The consequences are undefined if any accessor-name is not the name of an accessor for the instance.

See Also:

with-slots, symbol-macrolet

Notes:

A with-accessors expression of the form:

(with-accessors (slot-entry1. . . slot-entryn) instance-form form1. . . formk)

expands into the equivalent of

(let ((in instance-form))

(symbol-macrolet (Q1. . . Qn) form1. . . formk))

where Qiis

(variable-namei () (accessor-namei in))

Expanded Reference: with-accessors

Basic Usage

with-accessors is similar to with-slots, but references go through accessor generic functions instead of slot-value. Each entry is of the form (variable-name accessor-name).

(defclass person ()
((name :initarg :name :accessor person-name)
(age :initarg :age :accessor person-age)))

(let ((p (make-instance 'person :name "Alice" :age 30)))
(with-accessors ((n person-name) (a person-age)) p
(list n a)))
=> ("Alice" 30)

Modifying Through Accessors

Writes through with-accessors call the setf accessor, which means :before and :after methods on the writer are invoked.

(defclass person ()
((name :initarg :name :accessor person-name)
(age :initarg :age :accessor person-age)))

(let ((p (make-instance 'person :name "Bob" :age 25)))
(with-accessors ((n person-name) (a person-age)) p
(setf n "Robert")
(incf a 5))
(list (person-name p) (person-age p)))
=> ("Robert" 30)

Difference from with-slots

The key difference is that with-accessors invokes accessor methods (which may have :before/:after methods), while with-slots goes directly through slot-value.

(defclass tracked ()
((value :initarg :value :accessor tracked-value)))

(defmethod (setf tracked-value) :before (new-val (obj tracked))
(format t "Setting value to ~A~%" new-val))

;; with-accessors triggers the :before method
(let ((obj (make-instance 'tracked :value 0)))
(with-accessors ((v tracked-value)) obj
(setf v 42)))
.. Setting value to 42
..
=> 42

;; with-slots bypasses accessor methods
(let ((obj (make-instance 'tracked :value 0)))
(with-slots (value) obj
(setf value 42)))
=> 42

Working with Multiple Objects

Like with-slots, with-accessors forms can be nested to work with multiple objects simultaneously.

(defclass point ()
((x :initarg :x :accessor point-x)
(y :initarg :y :accessor point-y)))

(defun swap-points (p1 p2)
"Swap the coordinates of two points."
(with-accessors ((x1 point-x) (y1 point-y)) p1
(with-accessors ((x2 point-x) (y2 point-y)) p2
(psetf x1 x2 x2 x1
y1 y2 y2 y1))))

(let ((a (make-instance 'point :x 1 :y 2))
(b (make-instance 'point :x 3 :y 4)))
(swap-points a b)
(list (point-x a) (point-y a) (point-x b) (point-y b)))
=> (3 4 1 2)