Control structures in SuperCollider are implemented via message sends. Here are a few of those available. See Syntax Shortcuts for the various ways expressions can be written.
Conditional execution is implemented via the if
message. The if
message is sent to an expression which must return a Boolean value.
In addition it takes two arguments: a function to execute if the expression is true and another optional function to execute if the expression is false. The if
message returns the value of the function which is executed. If the falseFunc is not present and the expression is false then the result of the if message is nil.
Syntax
Examples
The while message implements conditional execution of a loop. If the testFunc answers true when evaluated, then the loopFunc is evaluated and the process is repeated. Once the testFunc returns false, the loop terminates.
Syntax
Example
while expressions are also optimized by the compiler if they do not contain variable declarations in the testFunc and the loopFunc.
The for message implements iteration over an integer series from a starting value to an end value stepping by one each time. A function is evaluated each iteration and is passed the iterated numeric value as an argument.
Syntax
Example
The forBy selector implements iteration over an integer series with a variable step size. A function is evaluated each iteration and is passed the iterated numeric value as an argument.
Syntax
Example
Do is used to iterate over a Collection. Positive Integers also respond to do
by iterating from zero up to their value. Collections iterate, calling the function for each object they contain. Other kinds of Objects respond to do by passing themselves to the function one time. The function is called with two arguments, the item, and an iteration counter.
Syntax
Example
(8..20).do
uses an optimization to avoid generating an array that is used only for iteration (but which would be discarded thereafter). The return value of (8..20).do { |item| item.postln }
is 8, the starting value.
However, if do
is written as an infix binary operator, as in (8..20) do: { |item| item.postln }
, then it will generate the series as an array first, before calling Array:do. One side effect of this is that it is valid to do
over an infinite series within a routine only if do
is written as a method call. If it is written as a binary operator, you will get a "wrong type" error because the array must be finite.
Object implements a switch method which allows for conditional evaluation with multiple cases. These are implemented as pairs of test objects (tested using if this == test.value) and corresponding functions to be evaluated if true.
The switch statement will be inlined if the test objects are all Floats, Integers, Symbols, Chars, nil, false, true and if the functions have no variable or argument declarations. The inlined switch uses a hash lookup (which is faster than nested if statements), so it should be very fast and scale to any number of clauses.
switch
construction that is not inlined will test ==
, while one that is inlined will test ===
. See the examples. One implication is that strings should be avoided: switch(text) { "abc" } { ... }
may not match, even if text == "abc"
. Symbols are preferred.(2/3) == (1 - (1/3))
is false. Therefore floats should be avoided in switch
constructions.Syntax
Examples
or:
Inlined vs non-inlined comparison:
The identity comparison 1.0 === 1
is false: while 1.0 and 1 represent the same numeric value, one is floating point and the other is an integer, so they cannot be identical.
Function implements a case method which allows for conditional evaluation with multiple cases. Since the receiver represents the first case this can be simply written as pairs of test functions and corresponding functions to be evaluated if true. Case is inlined and is therefore just as efficient as nested if statements.
Example
Function implements a try
method which allows catching exceptions thrown by code and run the exception handler. For more information see Exception.
Example
Function implements a protect
method which allows protecting code from exceptions thrown by code. The difference with try
is the exception handler code is always run, even if there is no exception. If an exception occurs, it is rethrown after the exception handler block is run. For more information see Exception.
Example
Using Functions, many control structures can be defined like the ones above. In the class Collection: Iteration there are many more messages defined for iterating over Collections.
if
, while
, switch
and case
expressions are optimized (i.e. inlined) by the compiler if they do not contain variable declarations in the functions. The optimization plucks the code from the functions and uses a more efficient jump statement:
Failure to inline due to variable declarations:
You can switch on and off the above warning (see: LanguageConfig: *postInlineWarnings)