www.delorie.com/gnu/docs/emacs/cl_16.html   search  
Buy the book!

Common Lisp Extensions

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.2.1 Basic Setf

The setf macro is the most basic way to operate on generalized variables.

Special Form: setf [place form]...
This macro evaluates form and stores it in place, which must be a valid generalized variable form. If there are several place and form pairs, the assignments are done sequentially just as with setq. setf returns the value of the last form.

The following Lisp forms will work as generalized variables, and so may legally appear in the place argument of setf:

Using any forms other than these in the place argument to setf will signal an error.

The setf macro takes care to evaluate all subforms in the proper left-to-right order; for example,

(setf (aref vec (incf i)) i)

looks like it will evaluate (incf i) exactly once, before the following access to i; the setf expander will insert temporary variables as necessary to ensure that it does in fact work this way no matter what setf-method is defined for aref. (In this case, aset would be used and no such steps would be necessary since aset takes its arguments in a convenient order.)

However, if the place form is a macro which explicitly evaluates its arguments in an unusual order, this unusual order will be preserved. Adapting an example from Steele, given

(defmacro wrong-order (x y) (list 'aref y x))

the form (setf (wrong-order a b) 17) will evaluate b first, then a, just as in an actual call to wrong-order.

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003