Skip to main content

define-setf-expander

define-setf-expander Macro

Syntax:

define-setf-expander access-fn lambda-list

[[ {declaration}* | documentation ]] {form}*

! access-fn

Arguments and Values:

access-fn—a symbol that names a function or macro.

lambda-listmacro lambda list.

declaration—a declare expression; not evaluated.

documentation—a string; not evaluated.

forms—an implicit progn.

Description:

define-setf-expander specifies the means by which setf updates a place that is referenced by access-fn.

When setf is given a place that is specified in terms of access-fn and a new value for the place, it is expanded into a form that performs the appropriate update.

The lambda-list supports destructuring. See Section 3.4.4 (Macro Lambda Lists).

Documentation is attached to access-fn as a documentation string of kind setf.

define-setf-expander

Forms constitute the body of the setf expander definition and must compute the setf expansion for a call on setf that references the place by means of the given access-fn. The setf expander function is defined in the same lexical environment in which the define-setf-expander form appears. While forms are being executed, the variables in lambda-list are bound to parts of the place form. The

body forms (but not the lambda-list) in a define-setf-expander form are implicitly enclosed in a block whose name is access-fn.

The evaluation of forms must result in the five values described in Section 5.1.1.2 (Setf Expansions).

If a define-setf-expander form appears as a top level form, the compiler must make the setf expander available so that it may be used to expand calls to setf later on in the file. Programmers must ensure that the forms can be evaluated at compile time if the access-fn is used in a place later in the same file. The compiler must make these setf expanders available to compile-time calls to get-setf-expansion when its environment argument is a value received as the environment parameter of a macro.

Examples:

(defun lastguy (x) (car (last x))) *!* LASTGUY 
(define-setf-expander lastguy (x &environment env)
"Set the last element in a list to the given value."
(multiple-value-bind (dummies vals newval setter getter)
(get-setf-expansion x env)
(let ((store (gensym)))
(values dummies
vals
(,store)
(progn (rplaca (last ,getter) ,store) ,store)
(lastguy ,getter))))) *!* LASTGUY
(setq a (list ’a ’b ’c ’d)
b (list ’x)
c (list 1 2 3 (list 4 5 6))) *!* (1 2 3 (4 5 6))
(setf (lastguy a) 3) *!* 3
(setf (lastguy b) 7) *!* 7
(setf (lastguy (lastguy c)) ’lastguy-symbol) *!* LASTGUY-SYMBOL
a *!* (A B C 3)
b *!* (7)
c *!* (1 2 3 (4 5 LASTGUY-SYMBOL))
;;; Setf expander for the form (LDB bytespec int).
;;; Recall that the int form must itself be suitable for SETF.
(define-setf-expander ldb (bytespec int &environment env)
(multiple-value-bind (temps vals stores
store-form access-form)
(get-setf-expansion int env);Get setf expansion for int.
Data and Control


(let ((btemp (gensym)) ;Temp var for byte specifier.
(store (gensym)) ;Temp var for byte to store.
(stemp (first stores))) ;Temp var for int to store.
(if (cdr stores) (error "Can’t expand this."))
;;; Return the setf expansion for LDB as five values.
(values (cons btemp temps) ;Temporary variables.
(cons bytespec vals) ;Value forms.
(list store) ;Store variables.
(let ((,stemp (dpb ,store ,btemp ,access-form)))
,store-form
,store) ;Storing form.
(ldb ,btemp ,access-form) ;Accessing form.
))))

See Also:

setf, defsetf, documentation, get-setf-expansion, Section 3.4.11 (Syntactic Interaction of Documentation Strings and Declarations)

Notes:

define-setf-expander di↵ers from the long form of defsetf in that while the body is being executed the variables in lambda-list are bound to parts of the place form, not to temporary variables that will be bound to the values of such parts. In addition, define-setf-expander does not have defsetf’s

restriction that access-fn must be a function or a function-like macro; an arbitrary defmacro destructuring pattern is permitted in lambda-list.

Expanded Reference: define-setf-expander

Custom setf Expander for a Clamped Value

define-setf-expander gives full control over how setf expands for a given accessor. The body must return five values via get-setf-expansion protocol: temporary variables, value forms, store variables, the storing form, and the accessing form.

;; Accessor: (clamped-ref vector index min max)
;; setf stores the value clamped to [min, max]
(defun clamped-ref (vector index min max)
(let ((val (aref vector index)))
(max min (min max val))))

(define-setf-expander clamped-ref (vector index min max &environment env)
(multiple-value-bind (temps vals stores store-form access-form)
(get-setf-expansion vector env)
(declare (ignore stores store-form access-form))
(let ((v-vec (gensym "VEC"))
(v-idx (gensym "IDX"))
(v-min (gensym "MIN"))
(v-max (gensym "MAX"))
(v-store (gensym "STORE")))
(values (list* v-vec v-idx v-min v-max temps)
(list* vector index min max vals)
(list v-store)
`(setf (aref ,v-vec ,v-idx)
(max ,v-min (min ,v-max ,v-store)))
`(clamped-ref ,v-vec ,v-idx ,v-min ,v-max)))))

(let ((v (vector 0 0 0)))
(setf (clamped-ref v 1 0 10) 25)
v)
=> #(0 10 0)

setf Expander for a Computed Place

Here is a simpler example that makes (middle lst) a setf-able place targeting the middle element of a list.

(defun middle (lst)
(nth (floor (length lst) 2) lst))

(define-setf-expander middle (lst &environment env)
(multiple-value-bind (temps vals stores store-form access-form)
(get-setf-expansion lst env)
(declare (ignore stores store-form access-form))
(let ((v-lst (gensym "LST"))
(v-store (gensym "STORE")))
(values (list* v-lst temps)
(list* lst vals)
(list v-store)
`(setf (nth (floor (length ,v-lst) 2) ,v-lst) ,v-store)
`(middle ,v-lst)))))

(let ((data (list 1 2 3 4 5)))
(setf (middle data) 99)
data)
=> (1 2 99 4 5)