An Environment is an IdentityDictionary with additional features that allow it to serve as a 'name space' within which functions can be defined and/or evaluated.
Creates a new Environment and sends make message.
Creates a new Environment and sends use message.
Saves currentEnvironment on the stack.
Restores currentEnvironment from the stack.
Evaluates the function within the environment, returns the environment.
Evaluates the function within the environment, returns the return value of the function.
Saves the receiver on the stack.
Restores currentEnvironment from the stack.
Links the environment to the current document, so that it is the currentEnvironment only when one document is in focus. See: Document: -envir
The document to link to, defaults to the current document (Document: *current). If the document has focus or no document is given, the environment is pushed immediately.
Uninks the environment to the current document, so that it is the currentEnvironment only when one document is in focus. See: Document: -envir
The document to unlink from, defaults to the current document (Document: *current). If the document has focus or no document is given, the environment is popped immediately.
These are not methods, but global variables.
determines environment used by "~" syntax, valueEnvir, and valueArrayEnvir
initial value of currentEnvironment.
~environmentVariables placed here can be used as "global variables," as long as the topEnvironment remains the currentEnvironment.
Thus, ~abc is not truly a global variable because it is inaccessible if its environment is not current.
evaluates a function, looking up unspecified arguments in currentEnvironment
same as valueEnvir, but with arguments in an array
When SuperCollider starts, it creates an Environment that it stores in the pseudovariables topEnvironment and currentEnvironment. The topEnvironment provides a universally accessible collection of named values similar to the Interpreter variables a, b, c, ....
The compiler provides a shortcut syntax where ~ is a placeholder for currentEnvironment. This makes the expression
~myvariable; equivalent to
currentEnvironment.at(\myvariable); and the expression
~myvariable = 888; equivalent to
The messages *make(function) and *use(function) replace currentEnvironment with the receiver. The message *make is intended to be used when initializing an Environment, so it returns the Environment. The message *use is for evaluating a functions within an Environment, so it returns the return value of the function.
creates an environment, while
evaluates the function within that environment.
When Functions are evaluated with valueEnvir and valueArrayEnvir unspecified arguments are looked up in the current Environment. If the argument is not found in the Environment its default value is used.
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 environment.
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.
Asynchronous functions include any scheduled function, responder function associated with OSCFunc, MIDIFunc, HID or GUI action functions, or actions used in server messaging (such as Buffer.read, Buffer or Bus .get, and so on).
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.
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.
Environment's know variable holds a Boolean value controlling whether the Environment may be used as an object prototype or not. If know 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.)
The default for know is false for Environment, and true for Event.
More typically, Events are used to define such prototypes because the syntax is simpler.
An object prototype looks up the method selector in the Environment to decide what to do.
Most objects are simply returned -- the method call behaves like a getter for any other object.
If the selector is a setter, e.g. someVariable_(value) or someVariable = value, the new value is put into the Environment.
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.
The function may access objects in the Environment using the first function argument.
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.
If you wish to access objects in the environment using environment variable syntax, 'use' the environment within the function.
Assigning a value into an environment using a setter -- name_() or .name = ... -- posts a warning message if the name is already defined in the class library.