1
0
Fork 0
cl-sites/novaspec.org/cl/f_defsetf.html

794 lines
17 KiB
HTML
Raw Normal View History

2025-02-05 18:52:26 +01:00
<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>defsetf | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_defsetf.html"
><LINK REL="next" HREF="f_define-setf-expander.html" TYPE="text/html" TITLE="define-setf-expander"
><LINK REL="prev" HREF="f_define-modify-macro.html" TYPE="text/html" TITLE="define-modify-macro"
><LINK REL="up" HREF="5_3_Data_and_Control_Flow_Dictionary.html" TYPE="text/html" TITLE="5.3 Data and Control Flow Dictionary"
><LINK REL="start" HREF="index.html" TYPE="text/html" TITLE="Common Lisp Nova Spec"
><META NAME="VIEWPORT" CONTENT="width=device-width, initial-scale=1.0"
><LINK REL="STYLESHEET" HREF="dpans.css%3F3909942064.css"
><SCRIPT SRC="dpans.js%3F3909942064"
></SCRIPT
><SCRIPT SRC="apropos.js%3F3909942064"
></SCRIPT
></HEAD
><BODY
><DIV
><DIV CLASS="topnav"
><DIV CLASS="breadcrumb"
><SPAN CLASS="breadcrumb-item"
><A HREF="index.html"
>Common Lisp Nova Spec</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="5_Data_and_Control_Flow.html"
>5. Data and Control Flow</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="5_3_Data_and_Control_Flow_Dictionary.html"
>5.3 Data and Control Flow Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_defsetf.html"
>defsetf</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_define-modify-macro.html" CLASS="prev"
>&#8592;</A
><SPAN ID="apropos-label"
>Apropos </SPAN
><INPUT ID="apropos" AUTOFOCUS="AUTOFOCUS" PLACEHOLDER="Type here to search" ONINPUT="aproposInput(this);" ONKEYUP="aproposKeyup(event);" ONCHANGE="aproposChange(this);" ONFOCUS="aproposFocus(this);" ONFOCUSOUT="aproposFocusout(this);"
><A HREF="f_define-setf-expander.html" CLASS="next"
>&#8594;</A
></DIV
><DIV ID="apropos-res"
></DIV
></DIV
></DIV
><DIV CLASS="matter"
><DIV CLASS="com"
><DIV CLASS="begincom"
><HR
><TABLE WIDTH="100%" CELLSPACING="0" CELLPADDING="0"
><TR
><TD ALIGN="LEFT" VALIGN="BASELINE" WIDTH="100%" CLASS="name"
><SPAN CLASS="idx" DATA-KIND="idxref" DATA-TERM="defsetf"
></SPAN
><B
>defsetf</B
></TD
><TD ALIGN="RIGHT" VALIGN="BASELINE" WIDTH="0" NOWRAP="NOWRAP" CLASS="ftype"
><I
>Macro</I
></TD
></TR
></TABLE
><HR
></DIV
><UL CLASS="subtoc"
></UL
><DL
><DT
><B
>Syntax</B
></DT
><DD
><P CLASS="j"
>The &#8220;short form&#8221;:</P
><DIV CLASS="DefmacWithValuesNewline"
><DIV
><B
>defsetf</B
> <SPAN CLASS="cmssi"
>access-fn</SPAN
> <SPAN CLASS="cmssi"
>update-fn</SPAN
> <SPAN CLASS="cmr"
>[</SPAN
><SPAN CLASS="cmssi"
>documentation</SPAN
><SPAN CLASS="cmr"
>]</SPAN
> </DIV
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmssi"
>access-fn</SPAN
></DIV
></DIV
><P CLASS="j"
>The &#8220;long form&#8221;:</P
><DIV CLASS="DefmacWithValuesNewline"
><DIV
><B
>defsetf</B
> <SPAN CLASS="cmssi"
>access-fn</SPAN
> <SPAN CLASS="cmssi"
>lambda-list</SPAN
> <SPAN CLASS="cmr"
>(</SPAN
><SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>store-variable</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*)</SPAN
> <SPAN CLASS="cmr"
>&#10214;&#8201;</SPAN
><SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>declaration</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
> <SPAN CLASS="cmsy"
>|</SPAN
> <VAR CLASS="param"
>documentation</VAR
>&#8201;<SPAN CLASS="cmr"
>&#8201;&#10215;</SPAN
>&#8201;<SPAN CLASS="cmsy"
>{</SPAN
><VAR CLASS="param"
>form</VAR
><SPAN CLASS="cmsy"
>}</SPAN
><SPAN CLASS="cmr"
>*</SPAN
> </DIV
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmssi"
>access-fn</SPAN
></DIV
></DIV
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>access-fn</VAR
> &#8212; a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> which names a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> or a <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>update-fn</VAR
> &#8212; a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> naming a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> or <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>lambda-list</VAR
> &#8212; a <A HREF="26_1_Glossary.html#defsetf_lambda_list"
><EM CLASS="term"
>defsetf lambda list</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>store-variable</VAR
> &#8212; a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
> (a <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> <A HREF="26_1_Glossary.html#name"
><EM CLASS="term"
>name</EM
></A
>). </P
><P CLASS="j"
><VAR CLASS="param"
>declaration</VAR
> &#8212; a <A HREF="m_declare.html" CLASS="misc"
><B
>declare</B
></A
> <A HREF="26_1_Glossary.html#expression"
><EM CLASS="term"
>expression</EM
></A
>; not evaluated. </P
><P CLASS="j"
><VAR CLASS="param"
>documentation</VAR
> &#8212; a <A HREF="26_1_Glossary.html#string"
><EM CLASS="term"
>string</EM
></A
>; not evaluated. </P
><P CLASS="j"
><VAR CLASS="param"
>form</VAR
> &#8212; a <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
>.</P
></DD
><DT
><B
>Description</B
></DT
><DD
><P CLASS="j"
><A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> defines how to <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> a <A HREF="26_1_Glossary.html#place"
><EM CLASS="term"
>place</EM
></A
> of the form <CODE CLASS="f"
>(<I CLASS="i"
><I
>access-fn</I
></I
> ...)</CODE
> for relatively simple cases. (See <A HREF="f_define-setf-expander.html" CLASS="macref"
><B
>define-setf-expander</B
></A
> for more general access to this facility.) It must be the case that the <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> or <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
> named by <VAR CLASS="param"
>access-fn</VAR
> evaluates all of its arguments. </P
><P CLASS="j"
><A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> may take one of two forms, called the &#8220;short form&#8221; and the &#8220;long form,&#8221; which are distinguished by the <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> of the second <A HREF="26_1_Glossary.html#argument"
><EM CLASS="term"
>argument</EM
></A
>. </P
><P CLASS="j"
>When the short form is used, <VAR CLASS="param"
>update-fn</VAR
> must name a <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> (or <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
>) that takes one more argument than <VAR CLASS="param"
>access-fn</VAR
> takes. When <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> is given a <A HREF="26_1_Glossary.html#place"
><EM CLASS="term"
>place</EM
></A
> that is a call on <VAR CLASS="param"
>access-fn</VAR
>, it expands into a call on <VAR CLASS="param"
>update-fn</VAR
> that is given all the arguments to <VAR CLASS="param"
>access-fn</VAR
> and also, as its last argument, the new value (which must be returned by <VAR CLASS="param"
>update-fn</VAR
> as its value). </P
><P CLASS="j"
>The long form <A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> resembles <A HREF="f_defmacro.html" CLASS="macref"
><B
>defmacro</B
></A
>. The <VAR CLASS="param"
>lambda-list</VAR
> describes the arguments of <VAR CLASS="param"
>access-fn</VAR
>. The <VAR CLASS="param"
>store-variables</VAR
> describe the value or values to be stored into the <A HREF="26_1_Glossary.html#place"
><EM CLASS="term"
>place</EM
></A
>. The <VAR CLASS="param"
>body</VAR
> must compute the expansion of a <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> of a call on <VAR CLASS="param"
>access-fn</VAR
>. The expansion function is defined in the same <A HREF="26_1_Glossary.html#lexical_environment"
><EM CLASS="term"
>lexical environment</EM
></A
> in which the <A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> appears. </P
><P CLASS="j"
>During the evaluation of the <VAR CLASS="param"
>forms</VAR
>, the variables in the <VAR CLASS="param"
>lambda-list</VAR
> and the <VAR CLASS="param"
>store-variables</VAR
> are bound to names of temporary variables, generated as if by <A HREF="f_gensym.html" CLASS="funref"
><B
>gensym</B
></A
> or <A HREF="f_gentemp.html" CLASS="funref"
><B
>gentemp</B
></A
>, that will be bound by the expansion of <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> to the values of those <A HREF="26_1_Glossary.html#subform"
><EM CLASS="term"
>subforms</EM
></A
>. This binding permits the <VAR CLASS="param"
>forms</VAR
> to be written without regard for order-of-evaluation issues. <A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> arranges for the temporary variables to be optimized out of the final result in cases where that is possible. </P
><P CLASS="j"
>The body code in <A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> is implicitly enclosed in a <A HREF="26_1_Glossary.html#block"
><EM CLASS="term"
>block</EM
></A
> whose name is <VAR CLASS="param"
>access-fn</VAR
> </P
><P CLASS="j"
><A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> ensures that <A HREF="26_1_Glossary.html#subform"
><EM CLASS="term"
>subforms</EM
></A
> of the <A HREF="26_1_Glossary.html#place"
><EM CLASS="term"
>place</EM
></A
> are evaluated exactly once. </P
><P CLASS="j"
><VAR CLASS="param"
>Documentation</VAR
> is attached to <VAR CLASS="param"
>access-fn</VAR
> as a <A HREF="26_1_Glossary.html#documentation_string"
><EM CLASS="term"
>documentation string</EM
></A
> of kind <A HREF="f_setf.html" CLASS="misc"
><B
>setf</B
></A
>. </P
><P CLASS="j"
>If a <A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> appears as a <A HREF="26_1_Glossary.html#top_level_form"
><EM CLASS="term"
>top level form</EM
></A
>, the <A HREF="26_1_Glossary.html#compiler"
><EM CLASS="term"
>compiler</EM
></A
> must make the <A HREF="26_1_Glossary.html#setf_expander"
><EM CLASS="term"
>setf expander</EM
></A
> available so that it may be used to expand calls to <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> later on in the <A HREF="26_1_Glossary.html#file"
><EM CLASS="term"
>file</EM
></A
>. Users must ensure that the <VAR CLASS="param"
>forms</VAR
>, if any, can be evaluated at compile time if the <VAR CLASS="param"
>access-fn</VAR
> is used in a <A HREF="26_1_Glossary.html#place"
><EM CLASS="term"
>place</EM
></A
> later in the same <A HREF="26_1_Glossary.html#file"
><EM CLASS="term"
>file</EM
></A
>. The <A HREF="26_1_Glossary.html#compiler"
><EM CLASS="term"
>compiler</EM
></A
> must make these <A HREF="26_1_Glossary.html#setf_expander"
><EM CLASS="term"
>setf expanders</EM
></A
> available to compile-time calls to <A HREF="f_get-setf-expansion.html" CLASS="funref"
><B
>get-setf-expansion</B
></A
> when its <VAR CLASS="param"
>environment</VAR
> argument is a value received as the <A HREF="26_1_Glossary.html#environment_parameter"
><EM CLASS="term"
>environment parameter</EM
></A
> of a <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macro</EM
></A
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><P CLASS="j"
>The effect of</P
><PRE CLASS="screen"
>(defsetf symbol-value set)</PRE
><P CLASS="j"
>is built into the Common Lisp system. This causes the form <CODE CLASS="f"
>(setf (symbol-value foo) fu)</CODE
> to expand into <CODE CLASS="f"
>(set foo fu)</CODE
>. </P
><P CLASS="j"
>Note that</P
><PRE CLASS="screen"
>(defsetf car rplaca)</PRE
><P CLASS="j"
>would be incorrect because <A HREF="f_rplaca.html" CLASS="funref"
><B
>rplaca</B
></A
> does not return its last argument.</P
><PRE CLASS="screen"
>(defun middleguy (x) (nth (truncate (1- (list-length x)) 2) x)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> MIDDLEGUY
(defun set-middleguy (x v)
(unless (null x)
(rplaca (nthcdr (truncate (1- (list-length x)) 2) x) v))
v) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> SET-MIDDLEGUY
(defsetf middleguy set-middleguy) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> MIDDLEGUY
(setq a (list 'a 'b 'c 'd)
b (list 'x)
c (list 1 2 3 (list 4 5 6) 7 8 9)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 2 3 (4 5 6) 7 8 9)
(setf (middleguy a) 3) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3
(setf (middleguy b) 7) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 7
(setf (middleguy (middleguy c)) 'middleguy-symbol) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> MIDDLEGUY-SYMBOL
a <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (A 3 C D)
b <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (7)
c <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (1 2 3 (4 MIDDLEGUY-SYMBOL 6) 7 8 9)</PRE
><P CLASS="j"
>An example of the use of the long form of <A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
>:</P
><PRE CLASS="screen"
>(defsetf subseq (sequence start &amp;optional end) (new-sequence)
`(progn (replace ,sequence ,new-sequence
:start1 ,start :end1 ,end)
,new-sequence)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> SUBSEQ</PRE
><PRE CLASS="screen"
> (defvar *xy* (make-array '(10 10)))
(defun xy (&amp;key ((x x) 0) ((y y) 0)) (aref *xy* x y)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> XY
(defun set-xy (new-value &amp;key ((x x) 0) ((y y) 0))
(setf (aref *xy* x y) new-value)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> SET-XY
(defsetf xy (&amp;key ((x x) 0) ((y y) 0)) (store)
`(set-xy ,store 'x ,x 'y ,y)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> XY
(get-setf-expansion '(xy a b))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (#:t0 #:t1),
(a b),
(#:store),
((lambda (&amp;key ((x #:x)) ((y #:y)))
(set-xy #:store 'x #:x 'y #:y))
#:t0 #:t1),
(xy #:t0 #:t1)
(xy 'x 1) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> NIL
(setf (xy 'x 1) 1) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1
(xy 'x 1) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1
(let ((a 'x) (b 'y))
(setf (xy a 1 b 2) 3)
(setf (xy b 5 a 9) 14))
<SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 14
(xy 'y 0 'x 1) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 1
(xy 'x 1 'y 2) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> 3</PRE
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_documentation.html" CLASS="funref"
><B
>documentation</B
></A
>, <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
>, <A HREF="f_define-setf-expander.html" CLASS="macref"
><B
>define-setf-expander</B
></A
>, <A HREF="f_get-setf-expansion.html" CLASS="funref"
><B
>get-setf-expansion</B
></A
>, <A HREF="5_1_Generalized_Reference.html#sec_5_1" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>5.1</SPAN
> <SPAN CLASS="cmr"
>(Generalized</SPAN
> <SPAN CLASS="cmr"
>Reference)</SPAN
></A
>, <A HREF="3_4_Lambda_Lists.html#sec_3_4_11" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.4.11</SPAN
> <SPAN CLASS="cmr"
>(Syntactic</SPAN
> <SPAN CLASS="cmr"
>Interaction</SPAN
> <SPAN CLASS="cmr"
>of</SPAN
> <SPAN CLASS="cmr"
>Documentation</SPAN
> <SPAN CLASS="cmr"
>Strings</SPAN
> <SPAN CLASS="cmr"
>and</SPAN
> <SPAN CLASS="cmr"
>Declarations)</SPAN
></A
></P
></DD
><DT
><B
>Notes</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>forms</VAR
> must include provision for returning the correct value (the value or values of <VAR CLASS="param"
>store-variable</VAR
>). This is handled by <VAR CLASS="param"
>forms</VAR
> rather than by <A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> because in many cases this value can be returned at no extra cost, by calling a function that simultaneously stores into the <A HREF="26_1_Glossary.html#place"
><EM CLASS="term"
>place</EM
></A
> and returns the correct value. </P
><P CLASS="j"
>A <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> of a call on <VAR CLASS="param"
>access-fn</VAR
> also evaluates all of <VAR CLASS="param"
>access-fn</VAR
>&#8217;s arguments; it cannot treat any of them specially. This means that <A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> cannot be used to describe how to store into a <A HREF="26_1_Glossary.html#generalized_reference"
><EM CLASS="term"
>generalized reference</EM
></A
> to a byte, such as <CODE CLASS="f"
>(ldb field reference)</CODE
>. <A HREF="f_define-setf-expander.html" CLASS="macref"
><B
>define-setf-expander</B
></A
> is used to handle situations that do not fit the restrictions imposed by <A HREF="f_defsetf.html" CLASS="macref"
><B
>defsetf</B
></A
> and gives the user additional control.</P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_define-modify-macro.html" CLASS="prev"
>&#8592;</A
><A HREF="f_define-setf-expander.html" CLASS="next"
>&#8594;</A
></DIV
><DIV CLASS="trail"
>Conversion to HTML copyright 2023 by Gilbert Baumann</DIV
></DIV
></DIV
><SCRIPT
>domReady();</SCRIPT
></BODY
></HTML
>