75 lines
7.6 KiB
HTML
75 lines
7.6 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: Section 3.3.4.1</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="03_cd.htm">
|
|
<LINK REL=UP HREF="03_cd.htm">
|
|
<LINK REL=NEXT HREF="03_d.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="03_cd.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="03_cd.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="03_d.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
|
|
|
|
<HR>
|
|
|
|
<H2>
|
|
3.3.4.1 Examples of Declaration Scope</H2> <P>
|
|
Here is an example illustrating the <A REL=DEFINITION HREF="26_glo_s.htm#scope"><I>scope</I></A> of <A REL=DEFINITION HREF="26_glo_b.htm#bound_declaration"><I>bound declarations</I></A>. <P>
|
|
<PRE>
|
|
(let ((x 1)) ;[1] 1st occurrence of x
|
|
(declare (special x)) ;[2] 2nd occurrence of x
|
|
(let ((x 2)) ;[3] 3rd occurrence of x
|
|
(let ((old-x x) ;[4] 4th occurrence of x
|
|
(x 3)) ;[5] 5th occurrence of x
|
|
(declare (special x)) ;[6] 6th occurrence of x
|
|
(list old-x x)))) ;[7] 7th occurrence of x
|
|
=> (2 3)
|
|
</PRE>
|
|
</TT> <P>
|
|
The first occurrence of <TT>x</TT> <A REL=DEFINITION HREF="26_glo_e.htm#establish"><I>establishes</I></A> a <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_binding"><I>dynamic binding</I></A> of <TT>x</TT> because of the <A REL=DEFINITION HREF="d_specia.htm#special"><B>special</B></A> <A REL=DEFINITION HREF="26_glo_d.htm#declaration"><I>declaration</I></A> for <TT>x</TT> in the second line. The third occurrence of <TT>x</TT> <A REL=DEFINITION HREF="26_glo_e.htm#establish"><I>establishes</I></A> a <A REL=DEFINITION HREF="26_glo_l.htm#lexical_binding"><I>lexical binding</I></A> of <TT>x</TT> (because there is no <A REL=DEFINITION HREF="d_specia.htm#special"><B>special</B></A> <A REL=DEFINITION HREF="26_glo_d.htm#declaration"><I>declaration</I></A> in the corresponding <A REL=DEFINITION HREF="s_let_l.htm#let"><B>let</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>). The fourth occurrence of <TT>x</TT> <I>x</I> is a reference to the <A REL=DEFINITION HREF="26_glo_l.htm#lexical_binding"><I>lexical binding</I></A> of <TT>x</TT> established in the third line. The fifth occurrence of <TT>x</TT> <A REL=DEFINITION HREF="26_glo_e.htm#establish"><I>establishes</I></A> a <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_binding"><I>dynamic binding</I></A> of <I>x</I> for the body of the <A REL=DEFINITION HREF="s_let_l.htm#let"><B>let</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> that begins on that line because of the <A REL=DEFINITION HREF="d_specia.htm#special"><B>special</B></A> <A REL=DEFINITION HREF="26_glo_d.htm#declaration"><I>declaration</I></A> for <TT>x</TT> in the sixth line. The reference to <TT>x</TT> in the fourth line is not affected by the <A REL=DEFINITION HREF="d_specia.htm#special"><B>special</B></A> <A REL=DEFINITION HREF="26_glo_d.htm#declaration"><I>declaration</I></A> in the sixth line because that reference is not within the ``would-be <A REL=DEFINITION HREF="26_glo_l.htm#lexical_scope"><I>lexical scope</I></A>'' of the <A REL=DEFINITION HREF="26_glo_v.htm#variable"><I>variable</I></A> <TT>x</TT> in the fifth line. The reference to <TT>x</TT> in the seventh line is a reference to the <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_binding"><I>dynamic binding</I></A> of <I>x</I> <I>established</I> in the fifth line. <P>
|
|
Here is another example, to illustrate the <A REL=DEFINITION HREF="26_glo_s.htm#scope"><I>scope</I></A> of a <A REL=DEFINITION HREF="26_glo_f.htm#free_declaration"><I>free declaration</I></A>. In the following: <P>
|
|
<PRE>
|
|
(lambda (&optional (x (foo 1))) ;[1]
|
|
(declare (notinline foo)) ;[2]
|
|
(foo x)) ;[3]
|
|
</PRE>
|
|
</TT> <P>
|
|
the <A REL=DEFINITION HREF="26_glo_c.htm#call"><I>call</I></A> to <TT>foo</TT> in the first line might be compiled inline even though the <A REL=DEFINITION HREF="26_glo_c.htm#call"><I>call</I></A> to <TT>foo</TT> in the third line must not be. This is because the <A REL=DEFINITION HREF="d_inline.htm#notinline"><B>notinline</B></A> <A REL=DEFINITION HREF="26_glo_d.htm#declaration"><I>declaration</I></A> for <TT>foo</TT> in the second line applies only to the body on the third line. In order to suppress inlining for both <A REL=DEFINITION HREF="26_glo_c.htm#call"><I>calls</I></A>, one might write: <P>
|
|
<PRE>
|
|
(locally (declare (notinline foo)) ;[1]
|
|
(lambda (&optional (x (foo 1))) ;[2]
|
|
(foo x))) ;[3]
|
|
</PRE>
|
|
</TT> <P>
|
|
or, alternatively: <P>
|
|
<PRE>
|
|
(lambda (&optional ;[1]
|
|
(x (locally (declare (notinline foo)) ;[2]
|
|
(foo 1)))) ;[3]
|
|
(declare (notinline foo)) ;[4]
|
|
(foo x)) ;[5]
|
|
</PRE>
|
|
</TT> <P>
|
|
Finally, here is an example that shows the <A REL=DEFINITION HREF="26_glo_s.htm#scope"><I>scope</I></A> of <A REL=DEFINITION HREF="26_glo_d.htm#declaration"><I>declarations</I></A> in an <A REL=DEFINITION HREF="26_glo_i.htm#iteration_form"><I>iteration form</I></A>. <P>
|
|
<PRE>
|
|
(let ((x 1)) ;[1]
|
|
(declare (special x)) ;[2]
|
|
(let ((x 2)) ;[3]
|
|
(dotimes (i x x) ;[4]
|
|
(declare (special x))))) ;[5]
|
|
=> 1
|
|
</PRE>
|
|
</TT> <P>
|
|
In this example, the first reference to <TT>x</TT> on the fourth line is to the <A REL=DEFINITION HREF="26_glo_l.htm#lexical_binding"><I>lexical binding</I></A> of <TT>x</TT> established on the third line. However, the second occurrence of <TT>x</TT> on the fourth line lies within the <A REL=DEFINITION HREF="26_glo_s.htm#scope"><I>scope</I></A> of the <A REL=DEFINITION HREF="26_glo_f.htm#free_declaration"><I>free declaration</I></A> on the fifth line (because this is the <I>result-form</I> of the <A REL=DEFINITION HREF="m_dotime.htm#dotimes"><B>dotimes</B></A>) and therefore refers to the <A REL=DEFINITION HREF="26_glo_d.htm#dynamic_binding"><I>dynamic binding</I></A> of <TT>x</TT>. <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>
|