1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Body/d_specia.htm
2024-04-01 10:24:07 +02:00

132 lines
8.2 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: Declaration SPECIAL</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="d_optimi.htm">
<LINK REL=UP HREF="c_evalua.htm">
<LINK REL=NEXT HREF="s_locall.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="d_optimi.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="c_evalua.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="s_locall.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<A NAME="special"><I>Declaration</I> <B>SPECIAL</B></A> <P>
<P><B>Syntax:</B><P>
<P>
<TT>(special </TT><I>var</I><TT></TT><B>*</B><TT>)</TT> <P>
<P><B>Arguments:</B><P>
<P>
<I>var</I>---a <A REL=DEFINITION HREF="26_glo_s.htm#symbol"><I>symbol</I></A>. <P>
<P><B>Valid Context:</B><P>
<P>
<A REL=DEFINITION HREF="26_glo_d.htm#declaration"><I>declaration</I></A> or <A REL=DEFINITION HREF="26_glo_p.htm#proclamation"><I>proclamation</I></A> <P>
<P><B>Binding Types Affected:</B><P>
<P>
<A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <P>
<P><B>Description:</B><P>
<P>
Specifies that all of the <I>vars</I> named are dynamic. This specifier affects variable <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> and affects references. All variable <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> affected are made to be dynamic <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A>, and affected variable references refer to the current dynamic <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A>. For example: <P>
<PRE>
(defun hack (thing *mod*) ;The binding of the parameter
(declare (special *mod*)) ; *mod* is visible to hack1,
(hack1 (car thing))) ; but not that of thing.
(defun hack1 (arg)
(declare (special *mod*)) ;Declare references to *mod*
;within hack1 to be special.
(if (atom arg) *mod*
(cons (hack1 (car arg)) (hack1 (cdr arg)))))
</PRE>
</TT> <P>
A <A REL=DEFINITION HREF="#special"><B>special</B></A> declaration does not affect inner <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> of a <I>var</I>; the inner <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> implicitly shadow a <A REL=DEFINITION HREF="#special"><B>special</B></A> declaration and must be explicitly re-declared to be <A REL=DEFINITION HREF="#special"><B>special</B></A>. <A REL=DEFINITION HREF="#special"><B>special</B></A> declarations never apply to function <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A>. <P>
<A REL=DEFINITION HREF="#special"><B>special</B></A> declarations can be either <A REL=DEFINITION HREF="26_glo_b.htm#bound_declaration"><I>bound declarations</I></A>, affecting both a binding and references, or <A REL=DEFINITION HREF="26_glo_f.htm#free_declaration"><I>free declarations</I></A>, affecting only references, depending on whether the declaration is attached to a variable binding. <P>
When used in a <A REL=DEFINITION HREF="26_glo_p.htm#proclamation"><I>proclamation</I></A>, a <A REL=DEFINITION HREF="#special"><B>special</B></A> <A REL=DEFINITION HREF="26_glo_d.htm#declaration_specifier"><I>declaration specifier</I></A> applies to all <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> as well as to all references of the mentioned variables. For example, after <P>
<PRE>
(declaim (special x))
</PRE>
</TT> <P>
then in a function definition such as <P>
<PRE>
(defun example (x) ...)
</PRE>
</TT> <P>
the parameter <TT>x</TT> is bound as a dynamic variable rather than as a lexical variable. <P>
<P><B>Examples:</B><P>
<P>
<PRE>
(defun declare-eg (y) ;this y is special
(declare (special y))
(let ((y t)) ;this y is lexical
(list y
(locally (declare (special y)) y)))) ;this y refers to the
;special binding of y
=&gt; DECLARE-EG
(declare-eg nil) =&gt; (T NIL)
</PRE>
</TT> <P>
<PRE>
(setf (symbol-value 'x) 6)
(defun foo (x) ;a lexical binding of x
(print x)
(let ((x (1+ x))) ;a special binding of x
(declare (special x)) ;and a lexical reference
(bar))
(1+ x))
(defun bar ()
(print (locally (declare (special x))
x)))
(foo 10)
&gt;&gt; 10
&gt;&gt; 11
=&gt; 11
</PRE>
</TT> <P>
<PRE>
(setf (symbol-value 'x) 6)
(defun bar (x y) ;[1] 1st occurrence of x
(let ((old-x x) ;[2] 2nd occurrence of x -- same as 1st occurrence
(x y)) ;[3] 3rd occurrence of x
(declare (special x))
(list old-x x)))
(bar 'first 'second) =&gt; (FIRST SECOND)
</PRE>
</TT> <P>
<PRE>
(defun few (x &amp;optional (y *foo*))
(declare (special *foo*))
...)
</PRE>
</TT> The reference to <TT>*foo*</TT> in the first line of this example is not <A REL=DEFINITION HREF="#special"><B>special</B></A> even though there is a <A REL=DEFINITION HREF="#special"><B>special</B></A> declaration in the second line. <P>
<PRE>
(declaim (special prosp)) =&gt; <A REL=DEFINITION HREF="26_glo_i.htm#implementation-dependent">implementation-dependent</A>
(setq prosp 1 reg 1) =&gt; 1
(let ((prosp 2) (reg 2)) ;the binding of prosp is special
(set 'prosp 3) (set 'reg 3) ;due to the preceding proclamation,
(list prosp reg)) ;whereas the variable reg is lexical
=&gt; (3 2)
(list prosp reg) =&gt; (1 3)
(declaim (special x)) ;x is always special.
(defun example (x y)
(declare (special y))
(let ((y 3) (x (* x 2)))
(print (+ y (locally (declare (special y)) y)))
(let ((y 4)) (declare (special y)) (foo x)))) =&gt; EXAMPLE
</PRE>
</TT> In the contorted code above, the outermost and innermost <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> of <TT>y</TT> are dynamic, but the middle binding is lexical. The two arguments to <TT>+</TT> are different, one being the value, which is <TT>3</TT>, of the lexical variable <TT>y</TT>, and the other being the value of the dynamic variable named <TT>y</TT> (a <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> of which happens, coincidentally, to lexically surround it at an outer level). All the <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>bindings</I></A> of <TT>x</TT> and references to <TT>x</TT> are dynamic, however, because of the proclamation that <TT>x</TT> is always <A REL=DEFINITION HREF="#special"><B>special</B></A>. <P>
<P><B>See Also:</B><P>
<P>
<A REL=DEFINITION HREF="m_defpar.htm#defparameter"><B>defparameter</B></A>, <A REL=DEFINITION HREF="m_defpar.htm#defvar"><B>defvar</B></A> <P>
<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>