Variables
These Controls operate on typed variables.
| Control | Result |
|---|---|
declare(name, type) | No value |
declare(name, type, initial) | No value |
set(name, value) | No value |
unset(name) | No value |
declare
Declares a variable in the current scope. name is a literal identifier. The
type expression is evaluated first, followed by the initializer if supplied.
The initializer must produce a value accepted by the declared type.
declare(score, integer, 0)
set(score, add(score, 10))
print(score) / 10
Without an initializer, the variable is uninitialized. Reading it is an error
until set assigns a value. An optional declaration is not automatically
initialized to none:
declare(answer, optional(string), none)
print(is_none(answer)) / true
Declaring the same name twice in one scope fails. A declaration in a child scope can shadow an outer variable; see block scopes.
set
Evaluates the value expression and assigns it to the nearest visible variable named by the first argument. The variable must exist and its type must accept the new value. Assigning a value does not change the variable’s declared type. The value expression is evaluated before the assignment checks, so its side effects may occur even if assignment fails.
declare(count, integer, 1)
{
set(count, 2)
}
print(count) / 2
Mutation of an enclosing variable affects that same variable; see mutation. To change part of a compound object, use set_at or set_field.
unset
Removes a variable’s assigned value while keeping its name and declared type.
The variable must exist. Reading it afterward fails until it is assigned again.
Calling unset on an already uninitialized variable is allowed.
declare(value, integer, 42)
unset(value)
set(value, 7)
print(value) / 7
This does not assign none or remove the declaration. See
absence values.