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

61 lines
7.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: Section 3.1.5</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_ad.htm">
<LINK REL=UP HREF="03_a.htm">
<LINK REL=NEXT HREF="03_af.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_ad.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="03_a.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="03_af.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>
3.1.5 Shadowing</H2> <P>
If two <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>forms</I></A> that <A REL=DEFINITION HREF="26_glo_e.htm#establish"><I>establish</I></A> <A REL=DEFINITION HREF="26_glo_l.htm#lexical_binding"><I>lexical bindings</I></A> with the same <A REL=DEFINITION HREF="26_glo_n.htm#name"><I>name</I></A> N are textually nested, then references to N within the inner <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> refer to the <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> established by the inner <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>; the inner <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> for N <A REL=DEFINITION HREF="26_glo_s.htm#shadow"><I>shadows</I></A> the outer <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> for N. Outside the inner <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> but inside the outer one, references to N refer to the <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> established by the outer <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A>. For example: <P>
<PRE>
(defun test (x z)
(let ((z (* x 2)))
(print z))
z)
</PRE>
</TT> The <A REL=DEFINITION HREF="26_glo_b.htm#binding"><I>binding</I></A> of the variable <TT>z</TT> by <A REL=DEFINITION HREF="s_let_l.htm#let"><B>let</B></A> shadows the <A REL=DEFINITION HREF="26_glo_p.htm#parameter"><I>parameter</I></A> binding for the function <TT>test</TT>. The reference to the variable <TT>z</TT> in the <A REL=DEFINITION HREF="f_wr_pr.htm#print"><B>print</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> refers to the <A REL=DEFINITION HREF="s_let_l.htm#let"><B>let</B></A> binding. The reference to <TT>z</TT> at the end of the function <TT>test</TT> refers to the <A REL=DEFINITION HREF="26_glo_p.htm#parameter"><I>parameter</I></A> named <TT>z</TT>. <P>
Constructs that are lexically scoped act as if new names were generated for each <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> on each execution. Therefore, dynamic shadowing cannot occur. For example: <P>
<PRE>
(defun contorted-example (f g x)
(if (= x 0)
(funcall f)
(block here
(+ 5 (contorted-example g
#'(lambda () (return-from here 4))
(- x 1))))))
</PRE>
</TT> Consider the call <TT>(contorted-example nil nil 2)</TT>. This produces <TT>4</TT>. During the course of execution, there are three calls to <TT>contorted-example</TT>, interleaved with two blocks: <P>
<PRE>
(contorted-example nil nil 2)
(block here1 ...)
(contorted-example nil #'(lambda () (return-from here1 4)) 1)
(block here2 ...)
(contorted-example #'(lambda () (return-from here1 4))
#'(lambda () (return-from here2 4))
0)
(funcall f)
where f =&gt; #'(lambda () (return-from here1 4))
(return-from here1 4)
</PRE>
</TT> At the time the <TT>funcall</TT> is executed there are two <A REL=DEFINITION HREF="s_block.htm#block"><B>block</B></A> <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit points</I></A> outstanding, each apparently named <TT>here</TT>. The <A REL=DEFINITION HREF="s_ret_fr.htm#return-from"><B>return-from</B></A> <A REL=DEFINITION HREF="26_glo_f.htm#form"><I>form</I></A> executed as a result of the <TT>funcall</TT> operation refers to the outer outstanding <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> (here1), not the inner one (here2). It refers to that <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> textually visible at the point of execution of <A REL=DEFINITION HREF="s_fn.htm#function"><B>function</B></A> (here abbreviated by the <TT>#'</TT> syntax) that resulted in creation of the <A REL=DEFINITION HREF="26_glo_f.htm#function"><I>function</I></A> <A REL=DEFINITION HREF="26_glo_o.htm#object"><I>object</I></A> actually invoked by <A REL=DEFINITION HREF="f_funcal.htm#funcall"><B>funcall</B></A>. <P>
If, in this example, one were to change the <TT>(funcall f)</TT> to <TT>(funcall g)</TT>, then the value of the call <TT>(contorted-example nil nil 2)</TT> would be <TT>9</TT>. The value would change because <A REL=DEFINITION HREF="f_funcal.htm#funcall"><B>funcall</B></A> would cause the execution of <TT>(return-from here</TT>2<TT> 4)</TT>, thereby causing a return from the inner <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> (here2). When that occurs, the value <TT>4</TT> is returned from the middle invocation of <TT>contorted-example</TT>, <TT>5</TT> is added to that to get <TT>9</TT>, and that value is returned from the outer block and the outermost call to <TT>contorted-example</TT>. The point is that the choice of <A REL=DEFINITION HREF="26_glo_e.htm#exit_point"><I>exit point</I></A> returned from has nothing to do with its being innermost or outermost; rather, it depends on the lexical environment that is packaged up with a <A REL=DEFINITION HREF="26_glo_l.htm#lambda_expression"><I>lambda expression</I></A> when <A REL=DEFINITION HREF="s_fn.htm#function"><B>function</B></A> is executed. <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>