108 lines
11 KiB
HTML
108 lines
11 KiB
HTML
<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
|
|
<HTML>
|
|
<HEAD>
|
|
<TITLE>CLHS: Macro DEFINE-SETF-EXPANDER</TITLE>
|
|
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
|
|
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
|
|
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
|
|
<LINK REL=TOP HREF="../Front/index.htm">
|
|
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
|
|
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
|
|
<LINK REL=PREV HREF="m_defset.htm">
|
|
<LINK REL=UP HREF="c_data_a.htm">
|
|
<LINK REL=NEXT HREF="f_get_se.htm">
|
|
</HEAD>
|
|
<BODY>
|
|
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="m_defset.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_data_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="f_get_se.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<A NAME="define-setf-expander"><I>Macro</I> <B>DEFINE-SETF-EXPANDER</B></A> <P>
|
|
<P>
|
|
<P><B>Syntax:</B><P>
|
|
<P>
|
|
|
|
<B>define-setf-expander</B> <I>access-fn lambda-list [[<I>declaration</I><B>*</B> | <I>documentation</I>]] <I>form</I><B>*</B></I><P> => <I>access-fn</I><P>
|
|
<P>
|
|
<P><B>Arguments and Values:</B><P>
|
|
<P>
|
|
<I>access-fn</I>---a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A> that <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>names</I></A> a <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> or <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A>. <P>
|
|
<I>lambda-list</I> -- <A REL=DEFINITION HREF="26_glo_m.htm#macro_lambda_list"><I>macro lambda list</I></A>. <P>
|
|
<I>declaration</I>---a <A REL=DEFINITION HREF="s_declar.htm#declare"><B>declare</B></A> <A REL=DEFINITION HREF="26_glo_e.htm#expression"><I>expression</I></A>; not evaluated. <P>
|
|
<I>documentation</I>---a <A REL=DEFINITION HREF="26_glo_s.htm#string"><I>string</I></A>; not evaluated. <P>
|
|
<I>forms</I>---an <A REL=DEFINITION HREF="26_glo_i.htm#implicit_progn"><I>implicit progn</I></A>. <P>
|
|
<P><B>Description:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#define-setf-expander"><B>define-setf-expander</B></A> specifies the means by which <A REL=DEFINITION HREF="m_setf_.htm#setf"><B>setf</B></A> updates a <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A> that is referenced by <I>access-fn</I>. <P>
|
|
When <A REL=DEFINITION HREF="m_setf_.htm#setf"><B>setf</B></A> is given a <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A> that is specified in terms of <I>access-fn</I> and a new value for the <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A>, it is expanded into a form that performs the appropriate update. <P>
|
|
The <I>lambda-list</I> supports destructuring. See <A REL=CHILD HREF="03_dd.htm">Section 3.4.4 (Macro Lambda Lists)</A>. <P>
|
|
<I>Documentation</I> is attached to <I>access-fn</I> as a <A REL=DEFINITION HREF="26_glo_d.htm#documentation_string"><I>documentation string</I></A> of kind <A REL=DEFINITION HREF="a_setf.htm#setf"><B>setf</B></A>. <P>
|
|
<I>Forms</I> constitute the body of the <A REL=DEFINITION HREF="26_glo_s.htm#setf_expander"><I>setf expander</I></A> definition and must compute the <A REL=DEFINITION HREF="26_glo_s.htm#setf_expansion"><I>setf expansion</I></A> for a call on <A REL=DEFINITION HREF="m_setf_.htm#setf"><B>setf</B></A> that references the <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A> by means of the given <I>access-fn</I>. The <A REL=DEFINITION HREF="26_glo_s.htm#setf_expander"><I>setf expander</I></A> function is defined in the same <A REL=DEFINITION HREF="26_glo_l.htm#lexical_environment"><I>lexical environment</I></A> in which the <A REL=DEFINITION HREF="#define-setf-expander"><B>define-setf-expander</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> appears. While <I>forms</I> are being executed, the variables in <I>lambda-list</I> are bound to parts of the <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. The body <I>forms</I> (but not the <I>lambda-list</I>) in a <A REL=DEFINITION HREF="#define-setf-expander"><B>define-setf-expander</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> are implicitly enclosed in a <A REL=DEFINITION HREF="26_glo_b.htm#block"><I>block</I></A> whose name is <I>access-fn</I>. <P>
|
|
The evaluation of <I>forms</I> must result in the five values described in <A REL=CHILD HREF="05_aab.htm">Section 5.1.1.2 (Setf Expansions)</A>. <P>
|
|
If a <A REL=DEFINITION HREF="#define-setf-expander"><B>define-setf-expander</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> appears as a <A REL=DEFINITION HREF="26_glo_t.htm#top_level_form"><I>top level form</I></A>, the <A REL=DEFINITION HREF="26_glo_c.htm#compiler"><I>compiler</I></A> must make the <A REL=DEFINITION HREF="26_glo_s.htm#setf_expander"><I>setf expander</I></A> available so that it may be used to expand calls to <A REL=DEFINITION HREF="m_setf_.htm#setf"><B>setf</B></A> later on in the <A REL=DEFINITION HREF="26_glo_f.htm#file"><I>file</I></A>. <A REL=DEFINITION HREF="26_glo_p.htm#programmer"><I>Programmers</I></A> must ensure that the <I>forms</I> can be evaluated at compile time if the <I>access-fn</I> is used in a <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A> later in the same <A REL=DEFINITION HREF="26_glo_f.htm#file"><I>file</I></A>. The <A REL=DEFINITION HREF="26_glo_c.htm#compiler"><I>compiler</I></A> must make these <A REL=DEFINITION HREF="26_glo_s.htm#setf_expander"><I>setf expanders</I></A> available to compile-time calls to <A REL=DEFINITION HREF="f_get_se.htm#get-setf-expansion"><B>get-setf-expansion</B></A> when its <I>environment</I> argument is a value received as the <A REL=DEFINITION HREF="26_glo_e.htm#environment_parameter"><I>environment parameter</I></A> of a <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A>. <P>
|
|
<P><B>Examples:</B><P>
|
|
|
|
<PRE>
|
|
(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))
|
|
</PRE>
|
|
</TT> <P>
|
|
|
|
<PRE>
|
|
;;; 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.
|
|
(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.
|
|
))))
|
|
</PRE>
|
|
</TT> <P>
|
|
<P><B>Affected By:</B> None.
|
|
<P>
|
|
<P><B>Exceptional Situations:</B> None.
|
|
<P>
|
|
<P><B>See Also:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="m_setf_.htm#setf"><B>setf</B></A>, <A REL=DEFINITION HREF="m_defset.htm#defsetf"><B>defsetf</B></A>, <A REL=DEFINITION HREF="f_docume.htm#documentation"><B>documentation</B></A>, <A REL=DEFINITION HREF="f_get_se.htm#get-setf-expansion"><B>get-setf-expansion</B></A>, <A REL=CHILD HREF="03_dk.htm">Section 3.4.11 (Syntactic Interaction of Documentation Strings and Declarations)</A> <P>
|
|
<P><B>Notes:</B><P>
|
|
<P>
|
|
<A REL=DEFINITION HREF="#define-setf-expander"><B>define-setf-expander</B></A> differs from the long form of <A REL=DEFINITION HREF="m_defset.htm#defsetf"><B>defsetf</B></A> in that while the body is being executed the <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variables</I></A> in <I>lambda-list</I> are bound to parts of the <A REL=DEFINITION HREF="26_glo_p.htm#place"><I>place</I></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>, not to temporary variables that will be bound to the values of such parts. In addition, <A REL=DEFINITION HREF="#define-setf-expander"><B>define-setf-expander</B></A> does not have <A REL=DEFINITION HREF="m_defset.htm#defsetf"><B>defsetf</B></A>'s restriction that <I>access-fn</I> must be a <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> or a function-like <A REL=DEFINITION HREF="26_glo_m.htm#macro"><I>macro</I></A>; an arbitrary <A REL=DEFINITION HREF="m_defmac.htm#defmacro"><B>defmacro</B></A> destructuring pattern is permitted in <I>lambda-list</I>. <P>
|
|
<P>
|
|
<P><HR>The following <A REL=META HREF="../Front/X3J13Iss.htm">X3J13 cleanup issues</A>, <I>not part of the specification</I>, apply to this section:<P><UL><LI> <A REL=CHILD HREF="../Issues/iss059.htm">COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS:CLARIFY</A><LI> <A REL=CHILD HREF="../Issues/iss105.htm">DEFMACRO-BLOCK-SCOPE:EXCLUDES-BINDINGS</A><LI> <A REL=CHILD HREF="../Issues/iss161.htm">FLET-IMPLICIT-BLOCK:YES</A><LI> <A REL=CHILD HREF="../Issues/iss104.htm">DEFINING-MACROS-NON-TOP-LEVEL:ALLOW</A><LI> <A REL=CHILD HREF="../Issues/iss135.htm">DOCUMENTATION-FUNCTION-BUGS:FIX</A><LI> <A REL=CHILD HREF="../Issues/iss308.htm">SETF-METHOD-VS-SETF-METHOD:RENAME-OLD-TERMS</A><LI> <A REL=CHILD HREF="../Issues/iss097.htm">DECLS-AND-DOC</A><P></UL><HR>
|
|
|
|
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
|
|
|
|
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
|
|
</BODY>
|
|
</HTML>
|