Skip to main content

Assignments

A value can be assigned to a variable using the = operator:

let x = 1
x = 2 // assignment

Compound assignment

It is also possible to perform compound assignment - which is a combination of an operator and an assignment:

let x = 1
x += 2 // addition assignment - shorthand for x = x + 2
// x will now be 3

The following compound assignment operators are supported:

x += 2 // addition assignment
x -= 2 // subtraction assignment
x *= 2 // multiplication assignment
x /= 2 // division assignment
x %= 2 // modulus assignment
x **= 2 // exponentiation assignment
x &= 0b101 // bitwise AND assignment
x |= 0b101 // bitwise OR assignment
x ^= 0b101 // bitwise XOR assignment
x <<= 2 // bitwise left shift assignment
x >>= 2 // bitwise right shift assignment
x ??= 2 // coalescing assignment

Chaining assignments

It is possible to chain assignments together:

let y = 123
let x = y = 456
// make x and y now both 456

The last assignment in a chain can be a compound assignment:

let y = 1
let x = y += 2
// x and y now both 3

Multi-value assignment

Multiple return values can be destructured into variables by providing a list of assignees separated by commas. Values can be ignored using _. If there are more return values than assignees, the extra return values will be ignored.

pub fn GiveMe3Numbers() -> x, y, z {
return 123, 456, 789
}
pub fn Example() {
let _, x = GiveMe3Numbers // sets x to 456
}

Destructuring assignment

Arrays can be destructured into variables using a destructuring assignment:

let [x, y] = [1, 2] // sets x to 1 and y to 2

Map can also be destructured into variables using a destructuring assignment:

let {a, potato=b} = {a=1, b=2} // sets a to 1 and potato to 2

Assigning a value to an Entity or Map key

Values of an Entity or Map can be assigned to using . followed by a key.

If the key begins with a lowercase letter, then a new key will be created on the entity with that name.

let this = Spawn
this.abc = 123 // assigns the value 123 to a new key `abc` on the entity
Transmission { %(this.abc) } // displays 123

If the key begins with an uppercase letter, then it must refer to a declaration, such as a Symbol, Field, Prop or Setter.