title:: Assignment Statements
categories:: Language
summary:: assigning values to variables

section:: Single Assignment

A single assignment assigns the value of an expression on the right hand side to a variable on the left hand side. A single assignment is in the form:
code::
<variable> = <an expression>
::
examples:
code::
x = [1, 2, 3, 4].rotate(1);
c = a + b;
::

A variable can be assigned a value at any time, including during declaration.

code::
(
// assignment in declaration
var one = 1;
var two = 2;
var four = two + 2;

// reassign
two = two + one;
two = "and now for something completely different"
two = nil;

)
::

In some cases, chains of reassignment are useful:
code::
(
{
	var sig;
	sig = SinOsc.ar(LFNoise0.kr(10).exprange(1, 1000));
	sig = SinOsc.ar(310, sig * 3);
	sig = SinOsc.ar(230, sig * 0.4);
	sig * 0.2
}.play
)
::

section:: Multiple Assignment

A multiple assignment statement assigns the elements of a link::Classes/Collection:: which is the result of an expression on the right hand side, to a list of variables on the left hand side.
A multiple assignment statement is preceded by the symbol code::#::. If the last variable on the left is preceded by three dots, then the entire remainder of the collection is assigned to that variable. There must be at least one variable name before the ellipsis.

The form of a multiple assignment is:
code::
# <list of variables> = <expression>
::
-- or --
code::
# <list of variables> ... <variable> = <expression>
::

examples:
code::
# a, b, c = [1, 2, 3, 4, 5, 6]; // afterwards a=1, b=2, c=3

# a, b ... c = [1, 2, 3, 4, 5, 6]; // afterwards a=1, b=2, c = [3, 4, 5, 6]

# ... a = [1, 2, 3, 4, 5, 6]; // ILLEGAL, just use:    a = [1, 2, 3, 4, 5, 6];
::

Multiple assignment is implemented using the 'at' method and the 'copyToEnd' method.
Your right hand side expression can return any object that responds to these messages.

section:: Instance Variable Assignment

The basic syntax for setting the value of an instance variable is to use the variable's setter method which is the name of the variable with an underscore appended.
code::
point.x_(5); // set point's x coordinate to 5
::
An alternative syntax is to use instance variable assignment.
code::
point.x = 5;
::
This type of assignment is translated to the first form by the compiler. The two syntaxes are equivalent.


section:: Series Assignment to an ArrayedCollection or List

There is a special syntax for doing assignments to a range of values in an link::Classes/ArrayedCollection:: or link::Classes/List::.
code::
a = (0,10..200);
a[5..10] = 1;  // series stepping by 1

a = (0,10..200);
a[7,9..13] = 1; // a series by any step size

a = (0,10..200);
a[..5] = 1;  // from zero to n

a = (0,10..200);
a[12..] = 1;  // from n to the end of the array

a = (0,10..200);
a[1,3..] = 1;  // a series to the end of the array
::

