Constants and reassignment
Keep a setting bound to the same value
Use const when a name should stay bound to the same value. This makes an accidental reassignment a mistake the compiler can catch.
const retries = 3
print(retries)
3
Other code can read retries. Assigning a different value to retries would be a compile error.
Change a field held by a constant binding
A constant binding to a dictionary keeps the dictionary itself assigned to that name.
const settings = {sound = true}
settings.sound = false
print(settings.sound)
false
The dictionary changed. The name settings still refers to the same dictionary.
Named function declarations and import aliases are immutable bindings. A const binding prevents reassignment, and a collection held through a const binding can still be mutated.
Names in different modules are independent. An outer module's scalar binding can be read from a function. To share mutable state with such a function, put state in a collection or use an enclosing function's captured variable. Name lookup can fall through to predeclared functions and type descriptors. Assignments are statements. A compound assignment reads its target before computing the new value. An indexed/member assignment evaluates the receiver and key before the right side and evaluates each component once.
const settings = {enabled = true}
settings.enabled = false
rate = 2
func charge(quantity):
return quantity * rate
assert(charge(3) == 6)
value = 42
assert(type(value) == i64)
value = convert(u16, value)
assert(type(value) == u16)
value = 'ready'
assert(type(value) == string)