1
0
Fork 0
cl-sites/novaspec.org/cl/f_define-symbol-macro.html
2025-02-05 18:52:26 +01:00

456 lines
No EOL
9.5 KiB
HTML

<!DOCTYPE HTML>
<HTML LANG="en-us"
><HEAD
><TITLE
>define-symbol-macro | Common Lisp Nova Spec</TITLE
><META CHARSET="US-ASCII"
><LINK REL="canonical" HREF="f_define-symbol-macro.html"
><LINK REL="next" HREF="f_symbol-macrolet.html" TYPE="text/html" TITLE="symbol-macrolet"
><LINK REL="prev" HREF="f_macroexpand.html" TYPE="text/html" TITLE="macroexpand, macroexpand-1"
><LINK REL="up" HREF="3_8_Evaluation_and_Compilation_Dictionary.html" TYPE="text/html" TITLE="3.8 Evaluation and Compilation 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="3_Evaluation_and_Compilation.html"
>3. Evaluation and Compilation</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="3_8_Evaluation_and_Compilation_Dictionary.html"
>3.8 Evaluation and Compilation Dictionary</A
></SPAN
> <SPAN CLASS="breadcrumb-item"
>&#8594; <A HREF="f_define-symbol-macro.html"
>define-symbol-macro</A
></SPAN
></DIV
><DIV CLASS="apropos"
><DIV CLASS="apropos-io"
><A HREF="f_macroexpand.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_symbol-macrolet.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="define-symbol-macro"
></SPAN
><B
>define-symbol-macro</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
><DIV CLASS="DefmacWithValuesNewline"
><DIV
><B
>define-symbol-macro</B
> <SPAN CLASS="cmssi"
>symbol</SPAN
> <SPAN CLASS="cmssi"
>expansion</SPAN
> </DIV
><DIV
><SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> <SPAN CLASS="cmssi"
>symbol</SPAN
></DIV
></DIV
></DD
><DT
><B
>Arguments and Values</B
></DT
><DD
><P CLASS="j"
><VAR CLASS="param"
>symbol</VAR
> &#8212; a <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
>. </P
><P CLASS="j"
><VAR CLASS="param"
>expansion</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"
>Provides a mechanism for globally affecting the <A HREF="26_1_Glossary.html#macro_expansion"
><EM CLASS="term"
>macro expansion</EM
></A
> of the indicated <VAR CLASS="param"
>symbol</VAR
>. </P
><P CLASS="j"
>Globally establishes an expansion function for the <A HREF="26_1_Glossary.html#symbol_macro"
><EM CLASS="term"
>symbol macro</EM
></A
> named by <VAR CLASS="param"
>symbol</VAR
>. The only guaranteed property of an expansion <A HREF="26_1_Glossary.html#function"
><EM CLASS="term"
>function</EM
></A
> for a <A HREF="26_1_Glossary.html#symbol_macro"
><EM CLASS="term"
>symbol macro</EM
></A
> is that when it is applied to the <A HREF="26_1_Glossary.html#form"
><EM CLASS="term"
>form</EM
></A
> and the <A HREF="26_1_Glossary.html#environment"
><EM CLASS="term"
>environment</EM
></A
> it returns the correct expansion. (In particular, it is <A HREF="26_1_Glossary.html#implementation-dependent"
><EM CLASS="term"
>implementation-dependent</EM
></A
> whether the expansion is conceptually stored in the expansion function, the <A HREF="26_1_Glossary.html#environment"
><EM CLASS="term"
>environment</EM
></A
>, or both.) </P
><P CLASS="j"
>Each global reference to <VAR CLASS="param"
>symbol</VAR
> (<I CLASS="i"
><I
>i.e.</I
></I
>, not <A HREF="26_1_Glossary.html#shadow"
><EM CLASS="term"
>shadowed</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>2</SPAN
></SUB
></SPAN
> by a <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>binding</EM
></A
> for a <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> or <A HREF="26_1_Glossary.html#symbol_macro"
><EM CLASS="term"
>symbol macro</EM
></A
> named by the same <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
>) is expanded by the normal macro expansion process; see <A HREF="3_1_Evaluation.html#sec_3_1_2_1_1" CLASS="secref"
><SPAN CLASS="cmr"
>Section</SPAN
> <SPAN CLASS="cmr"
>3.1.2.1.1</SPAN
> <SPAN CLASS="cmr"
>(Symbols</SPAN
> <SPAN CLASS="cmr"
>as</SPAN
> <SPAN CLASS="cmr"
>Forms)</SPAN
></A
>. The expansion of a <A HREF="26_1_Glossary.html#symbol_macro"
><EM CLASS="term"
>symbol macro</EM
></A
> is subject to further <A HREF="26_1_Glossary.html#macro_expansion"
><EM CLASS="term"
>macro expansion</EM
></A
> in the same <A HREF="26_1_Glossary.html#lexical_environment"
><EM CLASS="term"
>lexical environment</EM
></A
> as the <A HREF="26_1_Glossary.html#symbol_macro"
><EM CLASS="term"
>symbol macro</EM
></A
> reference, exactly analogous to normal <A HREF="26_1_Glossary.html#macro"
><EM CLASS="term"
>macros</EM
></A
>. </P
><P CLASS="j"
>The consequences are unspecified if a <A HREF="m_special.html" CLASS="declref"
><B
>special</B
></A
> declaration is made for <VAR CLASS="param"
>symbol</VAR
> while in the scope of this definition (<I CLASS="i"
><I
>i.e.</I
></I
>, when it is not <A HREF="26_1_Glossary.html#shadow"
><EM CLASS="term"
>shadowed</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>2</SPAN
></SUB
></SPAN
> by a <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>binding</EM
></A
> for a <A HREF="26_1_Glossary.html#variable"
><EM CLASS="term"
>variable</EM
></A
> or <A HREF="26_1_Glossary.html#symbol_macro"
><EM CLASS="term"
>symbol macro</EM
></A
> named by the same <A HREF="26_1_Glossary.html#symbol"
><EM CLASS="term"
>symbol</EM
></A
>). </P
><P CLASS="j"
>Any use of <A HREF="f_setq.html" CLASS="specref"
><B
>setq</B
></A
> to set the value of the <VAR CLASS="param"
>symbol</VAR
> while in the scope of this definition is treated as if it were a <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
>. <A HREF="f_psetq.html" CLASS="macref"
><B
>psetq</B
></A
> of <VAR CLASS="param"
>symbol</VAR
> is treated as if it were a <A HREF="f_setf.html" CLASS="macref"
><B
>psetf</B
></A
>, and <A HREF="f_multiple-value-setq.html" CLASS="macref"
><B
>multiple-value-setq</B
></A
> is treated as if it were a <A HREF="f_setf.html" CLASS="macref"
><B
>setf</B
></A
> of <A HREF="f_values.html" CLASS="funref"
><B
>values</B
></A
>. </P
><P CLASS="j"
>A <A HREF="26_1_Glossary.html#binding"
><EM CLASS="term"
>binding</EM
></A
> for a <A HREF="26_1_Glossary.html#symbol_macro"
><EM CLASS="term"
>symbol macro</EM
></A
> can be <A HREF="26_1_Glossary.html#shadow"
><EM CLASS="term"
>shadowed</EM
></A
><SPAN CLASS="meaning"
><SUB CLASS="sub"
><SPAN CLASS="cmr"
>2</SPAN
></SUB
></SPAN
> by <A HREF="f_let.html" CLASS="specref"
><B
>let</B
></A
> or <A HREF="f_symbol-macrolet.html" CLASS="specref"
><B
>symbol-macrolet</B
></A
>.</P
></DD
><DT
><B
>Examples</B
></DT
><DD
><PRE CLASS="screen"
>(defvar *things* (list 'alpha 'beta 'gamma)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> *THINGS*
(define-symbol-macro thing1 (first *things*)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> THING1
(define-symbol-macro thing2 (second *things*)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> THING2
(define-symbol-macro thing3 (third *things*)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> THING3
thing1 <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ALPHA
(setq thing1 'ONE) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> ONE
*things* <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (ONE BETA GAMMA)
(multiple-value-setq (thing2 thing3) (values 'two 'three)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> TWO
thing3 <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> THREE
*things* <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (ONE TWO THREE)
(list thing2 (let ((thing2 2)) thing2)) <SPAN CLASS="cmsy"
><SPAN CLASS="arrow"
>&#8594;</SPAN
></SPAN
> (TWO 2)</PRE
></DD
><DT
><B
>Exceptional Situations</B
></DT
><DD
><P CLASS="j"
>If <VAR CLASS="param"
>symbol</VAR
> is already defined as a <A HREF="26_1_Glossary.html#global_variable"
><EM CLASS="term"
>global variable</EM
></A
>, an error of <A HREF="26_1_Glossary.html#type"
><EM CLASS="term"
>type</EM
></A
> <A HREF="t_program-error.html" CLASS="typeref"
><B
>program-error</B
></A
> is signaled.</P
></DD
><DT
><B
>See Also</B
></DT
><DD
><P CLASS="j"
><A HREF="f_symbol-macrolet.html" CLASS="specref"
><B
>symbol-macrolet</B
></A
>, <A HREF="f_macroexpand.html" CLASS="funref"
><B
>macroexpand</B
></A
></P
></DD
></DL
></DIV
></DIV
><DIV CLASS="footer"
><DIV CLASS="btmnav"
><A HREF="f_macroexpand.html" CLASS="prev"
>&#8592;</A
><A HREF="f_symbol-macrolet.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
>