1 <!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
4 <meta http-equiv=
"Content-Type" content=
"text/html; charset=UTF-8">
5 <meta http-equiv=
"Content-Style-Type" content=
"text/css">
7 <meta name=
"Generator" content=
"Cocoa HTML Writer">
8 <meta name=
"CocoaVersion" content=
"824.48">
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: 14.0px Helvetica
}
14 p
.p5
{margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Helvetica
; min-height: 17.0px}
15 p
.p6
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
}
16 p
.p7
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; min-height: 12.0px}
17 p
.p8
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #c40000}
18 p
.p9
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #0010c4}
19 p
.p10
{margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Monaco
; min-height: 16.0px}
20 p
.p11
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #007200}
21 p
.p12
{margin: 0.0px 0.0px 0.0px 0.0px; font: 9.0px Monaco
; color: #606060}
22 span
.s1
{font: 12.0px Helvetica
}
23 span
.s2
{color: #007200}
24 span
.s3
{color: #0010c4}
25 span
.s4
{color: #000000}
26 span
.s5
{color: #c40000}
27 span
.s6
{color: #606060}
28 span
.s7
{color: #0016ff}
29 span
.Apple-tab-span
{white-space:pre
}
33 <p class=
"p1"><b>Environment
</b></p>
34 <p class=
"p2"><br></p>
35 <p class=
"p3"><b>superclass: IdentityDictionary
</b></p>
36 <p class=
"p3"><b>related classes: Event, IdentityDictionary
</b></p>
37 <p class=
"p2"><br></p>
38 <p class=
"p3">An Environment is an IdentityDictionary with additional features that allow it to serve as a 'name space' within
</p>
39 <p class=
"p3">which functions can be defined and/or evaluated.
</p>
40 <p class=
"p2"><br></p>
41 <p class=
"p3"><b>PseudoVariables (global variables)
</b></p>
42 <p class=
"p2"><br></p>
43 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>currentEnvironment
<span class=
"Apple-tab-span"> </span></b>determines environment used by
"~" syntax, valueEnvir, and valueArrayEnvir
</p>
44 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>topEnvironment
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b>initial value of currentEnvironment, can be used for 'global variables'
</p>
45 <p class=
"p2"><br></p>
46 <p class=
"p3"><b>Class variables
</b></p>
47 <p class=
"p2"><span class=
"Apple-tab-span"> </span></p>
48 <p class=
"p3"><span class=
"Apple-tab-span"> </span><b>stack
</b></p>
49 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b>Maintains a stack of Environments accessed by
<b>push
</b>and
<b>pop
</b></p>
50 <p class=
"p2"><br></p>
51 <p class=
"p3"><b>Class methods
</b></p>
52 <p class=
"p2"><br></p>
53 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>*make(function)
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b>creates a new Environment and sends make message
</p>
54 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>*use(function)
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b>creates a new Environment and sends use message
</p>
55 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>*push
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b>saves currentEnvironment on the stack
<span class=
"Apple-tab-span"> </span></p>
56 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>*pop
<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 class=
"Apple-tab-span"> </span></b>restores currentEnvironment from the stack
</p>
57 <p class=
"p2"><br></p>
58 <p class=
"p3"><b>Methods
</b></p>
59 <p class=
"p2"><br></p>
60 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>make(function)
</b><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>evaluates the function within the environment, returns the environment.
<span class=
"Apple-tab-span"> </span></p>
61 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>use(function)
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b>evaluates the function within the environment, returns the return valus of the function.
<span class=
"Apple-tab-span"> </span></p>
62 <p class=
"p3"><span class=
"Apple-tab-span"> </span><b>push
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b>saves the receiver on the stack
</p>
63 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>pop
<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 class=
"Apple-tab-span"> </span></b>restores currentEnvironment from the stack
</p>
64 <p class=
"p2"><br></p>
65 <p class=
"p3"><b>Related Messages
</b></p>
66 <p class=
"p2"><br></p>
67 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>valueEnvir (arg1, arg2...)
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></b>evaluates a function, looking up unspecified arguments in
<b>currentEnvironment
</b></p>
68 <p class=
"p3"><b><span class=
"Apple-tab-span"> </span>valueArrayEnvir (argArray)
<span class=
"Apple-tab-span"> </span></b>same as valueEnvir, but with arguments in an array
</p>
69 <p class=
"p2"><b><span class=
"Apple-tab-span"> </span></b></p>
70 <p class=
"p2"><br></p>
71 <p class=
"p4"><b>Overview: topEnvironment, currentEnvironment, make and use
</b></p>
72 <p class=
"p5"><br></p>
73 <p class=
"p2"><br></p>
74 <p class=
"p3">When SuperCollider starts, it creates an Environment that it stores in the pseudovariables
<span class=
"Apple-converted-space"> </span></p>
75 <p class=
"p3"><b>topEnvironment
</b>and
<b>currentEnvironment
</b>.
<span class=
"Apple-converted-space"> </span>The
<b>topEnvironment
</b>provides a universally
</p>
76 <p class=
"p3">accessible collection of named values
<span class=
"Apple-converted-space"> </span>similar to the
<b>Interpreter
</b>variables a, b, c, ....
</p>
77 <p class=
"p2"><br></p>
78 <p class=
"p3">The compiler provides a shortcut syntax where ~ is a placeholder for
<b>currentEnvironment
</b>.
<span class=
"Apple-converted-space"> </span></p>
79 <p class=
"p3">This makes the expression
</p>
80 <p class=
"p2"><br></p>
81 <p class=
"p6"><span class=
"Apple-tab-span"> </span>~myvariable;
<span class=
"Apple-converted-space"> <span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span><span class=
"s1">equivalent to
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>currentEnvironment.at(
<span class=
"s2">\myvariable
</span>);
</p>
82 <p class=
"p7"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
83 <p class=
"p3">and the expression
</p>
84 <p class=
"p2"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
85 <p class=
"p6"><span class=
"s1"><span class=
"Apple-tab-span"> </span></span>~myvariable =
888;
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s1">equivalent to
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>currentEnvironment.put(
<span class=
"s2">\myvariable
</span>,
888);
</p>
86 <p class=
"p2"><br></p>
87 <p class=
"p3">The messages
<b>make(function)
</b>and
<b>use(function)
</b>replace currentEnvironment with the receiver. evaluate
</p>
88 <p class=
"p3">the function and then restore currentEnvironment's original value.
<span class=
"Apple-converted-space"> </span>The message
<b>make
</b> is intended
</p>
89 <p class=
"p3">to be used when initializing an Environment, so it returns the Environment.
<span class=
"Apple-converted-space"> </span>The message
<b>use
</b>is for
</p>
90 <p class=
"p3">evaluating a functions within an Environment, so it returns the return value of the function.
</p>
91 <p class=
"p2"><br></p>
92 <p class=
"p3">For example
</p>
93 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>(
</p>
94 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>a =
<span class=
"s3">Environment
</span>.make({
</p>
95 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>~a =
100;
</p>
96 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>~b =
200;
</p>
97 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>~c =
300;
</p>
98 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>});
</p>
99 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>a.postln;
</p>
100 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>)
</p>
101 <p class=
"p3">creates an environment, while
</p>
102 <p class=
"p2"><br></p>
103 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>a.use({
</p>
104 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>~a + ~b + ~c
</p>
105 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>}).postln;
</p>
106 <p class=
"p7"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
107 <p class=
"p3">evaluates the function within that environment.
</p>
108 <p class=
"p2"><br></p>
109 <p class=
"p2"><br></p>
110 <p class=
"p4"><b>valueEnvir and valueArrayEnvir
</b></p>
111 <p class=
"p2"><br></p>
112 <p class=
"p3">When Functions are evaluated with
<b>valueEnvir
</b> and
<b>valueArrayEnvir
</b> unspecified arguments are looked up in the current Environment.
</p>
113 <p class=
"p3">If the argument is not found in the Environment its default value is used.
</p>
114 <p class=
"p2"><br></p>
116 <p class=
"p6"><span class=
"s3">var
</span> f;
</p>
117 <p class=
"p7"><br></p>
118 <p class=
"p8">// define a function
</p>
119 <p class=
"p6">f = {
<span class=
"s3">arg
</span> x, y, z; [x, y, z].postln; };
</p>
120 <p class=
"p7"><br></p>
121 <p class=
"p9">Environment
<span class=
"s4">.use({
</span></p>
122 <p class=
"p6"><span class=
"Apple-tab-span"> </span>~x =
7;
</p>
123 <p class=
"p6"><span class=
"Apple-tab-span"> </span>~y =
8;
</p>
124 <p class=
"p6"><span class=
"Apple-tab-span"> </span>~z =
9;
</p>
125 <p class=
"p7"><span class=
"Apple-tab-span"> </span></p>
126 <p class=
"p6"><span class=
"Apple-tab-span"> </span>f.valueEnvir(
1,
2,
3);
<span class=
"Apple-tab-span"> </span><span class=
"s5">// all values supplied
</span></p>
127 <p class=
"p8"><span class=
"s4"><span class=
"Apple-tab-span"> </span>f.valueEnvir(
1,
2);
<span class=
"Apple-tab-span"> </span></span>// z is looked up in the current Environment
</p>
128 <p class=
"p8"><span class=
"s4"><span class=
"Apple-tab-span"> </span>f.valueEnvir(
1);
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// y and z are looked up in the current Environment
<span class=
"Apple-converted-space"> </span></p>
129 <p class=
"p8"><span class=
"s4"><span class=
"Apple-tab-span"> </span>f.valueEnvir;
<span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// all arguments are looked up in the current Environment
</p>
130 <p class=
"p8"><span class=
"s4"><span class=
"Apple-tab-span"> </span>f.valueEnvir(z:
1);
<span class=
"Apple-tab-span"> </span></span>// x and y are looked up in the current Environment
</p>
131 <p class=
"p6">});
</p>
133 <p class=
"p10"><br></p>
134 <p class=
"p3">Now here is how this can be used with an instrument function. Environments allow you to define instruments without having to worry about argument ordering conflicts. Even though the three functions below have the freq, amp and pan args declared in different orders it does not matter, because valueEnvir looks them up in the
</p>
135 <p class=
"p3">environment.
<span class=
"Apple-converted-space"> </span></p>
136 <p class=
"p2"><br></p>
137 <p class=
"p6">s.boot;
</p>
138 <p class=
"p7"><br></p>
140 <p class=
"p6"><span class=
"s3">var
</span> orc;
</p>
141 <p class=
"p6">orc =
<span class=
"s3">Environment
</span>.make {
</p>
142 <p class=
"p6"><span class=
"Apple-tab-span"> </span>~a = {
<span class=
"s3">arg
</span> freq, amp, pan;
</p>
143 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s3">Pan2
</span>.ar(
<span class=
"s3">SinOsc
</span>.ar(freq), pan, amp);
</p>
144 <p class=
"p6"><span class=
"Apple-tab-span"> </span>};
</p>
145 <p class=
"p6"><span class=
"Apple-tab-span"> </span>~b =
<span class=
"Apple-converted-space"> </span>{
<span class=
"s3">arg
</span> amp, pan, freq;
</p>
146 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s3">Pan2
</span>.ar(
<span class=
"s3">RLPF
</span>.ar(
<span class=
"s3">Saw
</span>.ar(freq), freq *
6,
0.1), pan, amp);
</p>
147 <p class=
"p6"><span class=
"Apple-tab-span"> </span>};
</p>
148 <p class=
"p6"><span class=
"Apple-tab-span"> </span>~c =
<span class=
"Apple-converted-space"> </span>{
<span class=
"s3">arg
</span> pan, freq, amp;
</p>
149 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"s3">Pan2
</span>.ar(
<span class=
"s3">Resonz
</span>.ar(
<span class=
"s3">GrayNoise
</span>.ar, freq *
2,
0.1), pan, amp *
2);
</p>
150 <p class=
"p6"><span class=
"Apple-tab-span"> </span>};
</p>
151 <p class=
"p6"><span class=
"Apple-tab-span"> </span>~orc = [~a, ~b, ~c];
</p>
153 <p class=
"p8">// 'reverb'
</p>
154 <p class=
"p6">{
<span class=
"s3">var
</span> in; in =
<span class=
"s3">In
</span>.ar(
0,
2);
<span class=
"s3">CombN
</span>.ar(in,
0.2,
0.2,
3,
1, in); }.play(addAction:
<span class=
"s2">\addToTail
</span>);
</p>
155 <p class=
"p7"><br></p>
156 <p class=
"p6">{ loop({
</p>
157 <p class=
"p6"><span class=
"Apple-tab-span"> </span>orc.use({
</p>
158 <p class=
"p8"><span class=
"s4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// set values in the environment
</p>
159 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>~freq = exprand(
80,
600);
</p>
160 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>~amp =
0.1;
</p>
161 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span>~pan =
1.0.rand2;
</p>
162 <p class=
"p7"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
163 <p class=
"p8"><span class=
"s4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// call a randomly chosen instrument function
<span class=
"Apple-converted-space"> </span></p>
164 <p class=
"p8"><span class=
"s4"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></span>// with values from the environment
</p>
165 <p class=
"p7"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span></p>
166 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> x = { ~orc.choose.valueEnvir; }.play(fadeTime:
0.2, addAction:
<span class=
"s2">\addToHead
</span>);
<span class=
"Apple-converted-space"> </span></p>
167 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> 0.2.wait;
<span class=
"Apple-converted-space"> </span></p>
168 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"Apple-tab-span"> </span> x.release(
0.2);
<span class=
"Apple-converted-space"> </span></p>
169 <p class=
"p6"><span class=
"Apple-tab-span"> </span>});
</p>
170 <p class=
"p6">}) }.fork;
</p>
171 <p class=
"p7"><span class=
"Apple-tab-span"> </span></p>
173 <p class=
"p7"><br></p>
174 <p class=
"p2"><br></p>
175 <p class=
"p4"><b>Environments and asynchronous functions
</b></p>
176 <p class=
"p2"><br></p>
177 <p class=
"p3">Local variables declared in functions, and class and instance variables, use lexical scope. That is, the context in which they are understood depends on where the declaration is read during compilation. Asynchronous functions -- any function that will execute outside (later than) the current execution flow -- carry their lexically scoped variables with them.
</p>
178 <p class=
"p2"><br></p>
179 <p class=
"p6">f = {
<span class=
"s3">var
</span> a =
<span class=
"s6">"got it"</span>; { a.postln }.defer(
0.5) };
</p>
180 <p class=
"p6">f.value;
</p>
181 <p class=
"p2"><br></p>
182 <p class=
"p3">Asynchronous functions include any scheduled function, responder function associated with OSCresponder, MIDIResponder, HID or GUI action functions, or actions used in server messaging (such as Buffer.read, Buffer or Bus .get, and so on).
</p>
183 <p class=
"p2"><br></p>
184 <p class=
"p3">Environment variables have dynamic scope; they are read from whichever environment is current, whether or not it was the current environment when the function was declared. For instance, the following fails because e is no longer the current environment when the deferred function wakes up.
</p>
185 <p class=
"p2"><br></p>
186 <p class=
"p6">e = (a:
<span class=
"s6">"got it"</span>, f: { { ~a.postln }.defer(
0.5) });
</p>
187 <p class=
"p6">e.use { e.f };
</p>
188 <p class=
"p2"><br></p>
189 <p class=
"p3">Function's inEnvir method attaches a function to a specific environment. If no environment is given, the current environment at the time of executing inEnvir is the default.
</p>
190 <p class=
"p2"><br></p>
191 <p class=
"p6">e = (a:
<span class=
"s6">"got it"</span>, f: { { ~a.postln }.inEnvir.defer(
0.5) });
</p>
192 <p class=
"p6">e.use { e.f };
</p>
193 <p class=
"p7"><br></p>
194 <p class=
"p7"><br></p>
195 <p class=
"p2"><br></p>
196 <p class=
"p2"><br></p>
197 <p class=
"p4"><b>Using Environments as object prototypes
</b></p>
198 <p class=
"p2"><br></p>
199 <p class=
"p3">Environment's
<b>know
</b> variable holds a Boolean value controlling whether the Environment may be used as an object prototype or not. If
<b>know
</b> is true, any messages sent to the Environment that it does not already understand will be relayed into items in the Environment. (If false, not-understood messages will produce a standard
"does not understand" error message.)
</p>
200 <p class=
"p2"><br></p>
201 <p class=
"p3">The default for
<b>know
</b> is false for Environment, and true for
<a href=
"Event.html"><span class=
"s7">Event
</span></a>.
</p>
202 <p class=
"p2"><br></p>
203 <p class=
"p9"><span class=
"s4">e =
</span>Environment
<span class=
"s4">[
</span></p>
204 <p class=
"p11"><span class=
"s4"><span class=
"Apple-tab-span"> </span></span>'someVariable'
<span class=
"s4"> -
> 5,
</span></p>
205 <p class=
"p6"><span class=
"Apple-tab-span"> </span><span class=
"s2">'printMe'
</span> -
> {
<span class=
"s3">|self, string|
</span> string.postln }
</p>
207 <p class=
"p7"><br></p>
208 <p class=
"p6">e.know =
<span class=
"s3">true
</span>;
</p>
209 <p class=
"p2"><br></p>
210 <p class=
"p3">More typically, Events are used to define such prototypes because the syntax is simpler.
</p>
211 <p class=
"p2"><br></p>
212 <p class=
"p6">e = (someVariable:
5, printMe: {
<span class=
"s3">|self, string|
</span> string.postln });
</p>
213 <p class=
"p2"><br></p>
214 <p class=
"p2"><br></p>
215 <p class=
"p3">An object prototype looks up the method selector in the Environment to decide what to do.
</p>
216 <p class=
"p2"><br></p>
217 <p class=
"p3">Most objects are simply returned -- the method call behaves like a getter for any other object.
</p>
218 <p class=
"p2"><br></p>
219 <p class=
"p6">e.someVariable;
</p>
220 <p class=
"p8">// same as
</p>
221 <p class=
"p11"><span class=
"s4">e.at(
</span>'someVariable'
<span class=
"s4">);
</span></p>
222 <p class=
"p11"><span class=
"s4">e[
</span>'someVariable'
<span class=
"s4">];
</span></p>
223 <p class=
"p2"><br></p>
224 <p class=
"p3">If the selector is a setter, e.g.
<b>someVariable_(value)
</b> or
<b>e.someVariable = value
</b>, the new value is put into the Environment.
</p>
225 <p class=
"p2"><br></p>
226 <p class=
"p6">e.someVariable =
10;
</p>
227 <p class=
"p8">// same as
</p>
228 <p class=
"p11"><span class=
"s4">e.put(
</span>'someVariable'
<span class=
"s4">,
10);
</span></p>
229 <p class=
"p2"><br></p>
230 <p class=
"p3">If the Environment item is a function, it is evaluated as if it were a method definition. The first argument passed into the function is the Environment that holds the function; arguments to the method call follow as the second, third etc. arguments passed into the function.
</p>
231 <p class=
"p2"><br></p>
232 <p class=
"p12"><span class=
"s4">e.printMe(
</span>"Oh hai wrldz"<span class=
"s4">);
</span></p>
233 <p class=
"p8">// same as
</p>
234 <p class=
"p6">e[
<span class=
"s2">'printMe'
</span>].value(e,
<span class=
"s6">"Oh hai wrldz"</span>);
</p>
235 <p class=
"p2"><br></p>
236 <p class=
"p3">The function may access objects in the Environment using the first function argument.
</p>
237 <p class=
"p2"><br></p>
238 <p class=
"p6">e.mul2 = {
<span class=
"s3">|z|
</span> z.someVariable *
2 };
</p>
239 <p class=
"p6">e.mul2;
</p>
240 <p class=
"p2"><br></p>
241 <p class=
"p3">Environment variables inside a function will refer to the currently active environment -- not to the Environment being addressed. This is to allow the object prototype to interact with the currentEnvironment.
</p>
242 <p class=
"p2"><br></p>
243 <p class=
"p6">e.mul2 = {
<span class=
"s3">|z|
</span> ~someVariable *
2 };
</p>
244 <p class=
"p8">// this will throw an error because ~someVariable is nil in the currentEnvironment
</p>
245 <p class=
"p6">e.mul2;
</p>
246 <p class=
"p2"><br></p>
247 <p class=
"p3">If you wish to access objects in the environment using environment variable syntax, 'use' the environment within the function.
</p>
248 <p class=
"p2"><br></p>
249 <p class=
"p6">e.mul2 = {
<span class=
"s3">|z|
</span> z.use { ~someVariable *
2 } };
</p>
250 <p class=
"p6">e.mul2;
</p>
251 <p class=
"p2"><br></p>
252 <p class=
"p2"><br></p>
253 <p class=
"p3"><b>IMPORTANT:
</b> Be careful to avoid method names that are defined in any of the superclasses of environment (or event). Object prototyping works by trapping method selectors that are not already defined as class library methods. Using a generic method selector such as 'stop' or 'reset' will cause the corresponding class library method to respond, and the items in the environment will never be checked.
</p>
254 <p class=
"p2"><br></p>
255 <p class=
"p3">Assigning a value into an environment using a setter --
<b>name_()
</b> or
<b>.name = ...
</b> -- posts a warning message if the name is already defined in the class library.
</p>
256 <p class=
"p2"><br></p>
257 <p class=
"p6">e.reset = {
<span class=
"s6">"My reset function"</span>.postln };
</p>
258 <p class=
"p7"><br></p>
259 <p class=
"p8">// prints:
</p>
260 <p class=
"p6">WARNING:
</p>
261 <p class=
"p6">'reset' exists a method name, so you can't use it as pseudo-method.
</p>
262 <p class=
"p7"><br></p>
263 <p class=
"p8">// this does NOT execute the reset function above
</p>
264 <p class=
"p8">// because Object:reset responds
</p>
265 <p class=
"p6">e.reset;
</p>
266 <p class=
"p2"><br></p>