Skip to main content

with-slots

with-slots Macro

Syntax:

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

→ {result}*

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

Arguments and Values:

slot-name—a slot name; not evaluated.

variable-name—a variable name; not evaluated.

instance-form—a form; evaluted to produce instance.

instance—an object.

declaration—a declare expression; not evaluated.

forms—an implicit progn.

results—the values returned by the forms.

Description:

The macro with-slots establishes a lexical environment for referring to the slots in the instance named by the given slot-names as though they were variables. Within such a context the value of the slot can be specified by using its slot name, as if it were a lexically bound variable. Both setf and setq can be used to set the value of the slot.

The macro with-slots translates an appearance of the slot name as a variable into a call to slot-value.

Examples:

(defclass thing () 


**with-slots**
((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 thing (make-instance ’thing :x 0 :y 1)) → #<THING 62310540>
(with-slots (x y) thing (incf x) (incf y))2
(values (thing-x thing) (thing-y thing)) → 1, 2
(setq thing1 (make-instance ’thing :x 1 :y 2)) → #<THING 43135676>
(setq thing2 (make-instance ’thing :x 7 :y 8)) → #<THING 43147374>
(with-slots ((x1 x) (y1 y))
thing1
(with-slots ((x2 x) (y2 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 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 slot-name is not the name of a slot in the instance.

See Also:

with-accessors, slot-value, symbol-macrolet

Notes:

A with-slots expression of the form:

(with-slots (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

(slot-entryi() (slot-value inslot-entryi))

if slot-entryiis a symbol and is

(variable-namei () (slot-value inslot-namei))

if slot-entryiis of the form

(variable-namei slot-namei)

Expanded Reference: with-slots

Basic Slot Access

with-slots establishes a lexical environment where slot names can be used as if they were variables. Reads and writes go through slot-value.

(defclass point ()
((x :initarg :x :initform 0)
(y :initarg :y :initform 0)))

(let ((p (make-instance 'point :x 3 :y 4)))
(with-slots (x y) p
(list x y)))
=> (3 4)

Modifying Slots

Both setf and setq can be used within with-slots to modify slot values.

(defclass point ()
((x :initarg :x :initform 0)
(y :initarg :y :initform 0)))

(let ((p (make-instance 'point :x 0 :y 0)))
(with-slots (x y) p
(setf x 10)
(setq y 20))
(list (slot-value p 'x) (slot-value p 'y)))
=> (10 20)

Renaming Slots to Local Variables

When a slot name would conflict with another variable, or for clarity, you can rename it using the (variable-name slot-name) form.

(defclass point ()
((x :initarg :x :initform 0)
(y :initarg :y :initform 0)))

(let ((p1 (make-instance 'point :x 1 :y 2))
(p2 (make-instance 'point :x 3 :y 4)))
(with-slots ((x1 x) (y1 y)) p1
(with-slots ((x2 x) (y2 y)) p2
;; Euclidean distance
(sqrt (+ (expt (- x2 x1) 2)
(expt (- y2 y1) 2))))))
=> 2.828427

Computing Derived Values

with-slots is convenient for formulas that reference multiple slots of the same object.

(defclass rectangle ()
((width :initarg :width :initform 0)
(height :initarg :height :initform 0)))

(defgeneric area (shape))
(defgeneric perimeter (shape))

(defmethod area ((r rectangle))
(with-slots (width height) r
(* width height)))

(defmethod perimeter ((r rectangle))
(with-slots (width height) r
(* 2 (+ width height))))

(let ((r (make-instance 'rectangle :width 5 :height 3)))
(list (area r) (perimeter r)))
=> (15 16)

Swapping Slot Values

Because the slot references are genuine places, you can use rotatef and psetf within with-slots.

(defclass point ()
((x :initarg :x :initform 0)
(y :initarg :y :initform 0)))

(let ((p (make-instance 'point :x 10 :y 20)))
(with-slots (x y) p
(rotatef x y))
(list (slot-value p 'x) (slot-value p 'y)))
=> (20 10)