SCDoc: Use proper static string constants instead of comparing string literals.
[supercollider.git] / Help / Language / Scope.html
blobd896a4eb595b7076b4fd5918895cea16e28446b4
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2 <html>
3 <head>
4 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
5 <meta http-equiv="Content-Style-Type" content="text/css">
6 <title></title>
7 <meta name="Generator" content="Cocoa HTML Writer">
8 <meta name="CocoaVersion" content="824.42">
9 <style type="text/css">
10 p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 18.0px Helvetica}
11 p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}
12 p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}
13 p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco}
14 p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; color: #a71e12}
15 p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco; min-height: 12.0px}
16 p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Helvetica; min-height: 11.0px}
17 span.s1 {font: 12.0px Monaco}
18 span.s2 {color: #0019b7}
19 span.s3 {color: #000000}
20 span.s4 {color: #a71e12}
21 span.Apple-tab-span {white-space:pre}
22 </style>
23 </head>
24 <body>
25 <p class="p1"><b>Scoping and Closure<span class="Apple-converted-space"> </span></b></p>
26 <p class="p2"><br></p>
27 <p class="p3">SuperCollider has nested scoping of variables. A function can refer not only to its own arguments and variables, but also to those declared in any enclosing (defining) contexts.</p>
28 <p class="p2"><br></p>
29 <p class="p3">For example :</p>
30 <p class="p2"><br></p>
31 <p class="p3">The function defined below within <span class="s1">makeCounter</span> can access all of the arguments and variables declared in <span class="s1">makeCounter</span>. Other code can call the returned function at some later time and it can access and update the values contained in <span class="s1">makeCounter</span> at the time when the inner function was instantiated.<span class="Apple-converted-space"> </span></p>
32 <p class="p2"><br></p>
33 <p class="p4">(</p>
34 <p class="p4"><span class="s2">var</span> makeCounter;</p>
35 <p class="p4">makeCounter = { <span class="s2">arg</span> curVal=0, stepVal=1;<span class="Apple-converted-space"> </span></p>
36 <p class="p5"><span class="s3"><span class="Apple-tab-span"> </span> </span>// return a function :</p>
37 <p class="p4"><span class="Apple-tab-span"> </span>{</p>
38 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="s2">var</span> temp;</p>
39 <p class="p5"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// temp is local to this function, curVal &amp; stepVal in the</p>
40 <p class="p5"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// enclosing function are referred to here within.</p>
41 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>temp = curVal;</p>
42 <p class="p4"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>curVal = curVal + stepVal;</p>
43 <p class="p5"><span class="s3"><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span>temp <span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span><span class="Apple-tab-span"> </span></span>// return result</p>
44 <p class="p4"><span class="Apple-tab-span"> </span>}</p>
45 <p class="p4">};</p>
46 <p class="p6"><br></p>
47 <p class="p5">// each invocation of makeCounter creates a new set of variables curVal and stepVal</p>
48 <p class="p6"><br></p>
49 <p class="p4">x = makeCounter.value(10, 1);</p>
50 <p class="p4">z = makeCounter.value(99, 100);<span class="Apple-converted-space"> </span></p>
51 <p class="p6"><br></p>
52 <p class="p5">// x and z are functions which refer to different instances of the variables curVal and stepVal</p>
53 <p class="p6"><br></p>
54 <p class="p4">x.value.postln; <span class="s4">// posts 10</span></p>
55 <p class="p4">x.value.postln; <span class="s4">// posts 11</span></p>
56 <p class="p4">z.value.postln; <span class="s4">// posts 99</span></p>
57 <p class="p4">z.value.postln; <span class="s4">// posts 199</span></p>
58 <p class="p4">x.value.postln; <span class="s4">// posts 12</span></p>
59 <p class="p4">x.value.postln; <span class="s4">// posts 13</span></p>
60 <p class="p4">z.value.postln; <span class="s4">// posts 299</span></p>
61 <p class="p4">z.value.postln; <span class="s4">// posts 399</span></p>
62 <p class="p6"><br></p>
63 <p class="p4">)</p>
64 <p class="p7"><br></p>
65 <p class="p3">Note that even though the function which defines curVal and stepVal has completed execution, its variables are still accessible to those functions that were defined within its context. This is known as <b>lexical closure</b>, the capturing and availability of variables defined in outer contexts by inner contexts even when the outer contexts may have completed execution.</p>
66 <p class="p2"><br></p>
67 <p class="p2"><br></p>
68 <p class="p2"><br></p>
69 <p class="p2"><br></p>
70 <p class="p2"><br></p>
71 <p class="p7"><br></p>
72 <p class="p7"><br></p>
73 </body>
74 </html>