Values and Mutability
Bindings, assignment, aliasing, parameters, globals, and scope.
Contents
Const and Var#
A const binding cannot be reassigned. A var binding can be. Both require an initializer.
const name = "Ada"
var count = 0
count = count + 1
const fixed = [1, 2, 3]
// fixed = [4, 5, 6] // error: cannot assign to 'fixed'
A type annotation follows the name after a colon. An empty list literal requires one.
var values List[i32] = []
var total i64 = 0
const label str = "total"
A declaration may omit the colon when the type is followed directly by =, as in var total i64 = 0. The formatter writes the colon form for variables and the space form for typed globals.
Assignment and Mutation#
Assignment to a binding replaces the value at that location. Mutating a field or index changes the object the binding refers to.
type Counter {
n i32
}
var first = Counter { n = 1 }
var second = first
second.n = 9
print(first.n) // 9
The same rule applies to lists.
var left = [1, 2]
var right = left
right[0] = 9
print(left[0]) // 9
right = [7, 8]
print(left[0]) // 9
Scalars are copied on assignment. Managed values are references, so assignment copies the reference.
| Value category | Assignment copies | Mutation visible through aliases | Rebinding visible through aliases |
|---|---|---|---|
| Integer, float, bool, rune, enum | The value | Not applicable | No |
str |
The string reference | Strings are immutable | No |
Record, sum, Option, Result |
The reference | Yes | No |
List[T], bytes |
The reference | Yes | No |
| Closure | The reference | Captured state is by value; object state may be shared | No |
ptr, cstr, extern record |
The address or C-layout value | Depends on the pointed-to memory | No |
Mutation and Invariants#
A record with a cross-field invariant cannot have a single field assigned. Replace the whole value.
type Positive = i32 : invariant self > 0
type Window : invariant lo <= hi {
lo Positive
hi Positive
}
fn widen(w Window, by i32) Result[Window, str] {
const lo = Positive { w.lo - by }?
const hi = Positive { w.hi + by }?
return Success(Window { lo = lo, hi = hi }?)
}
A record without a cross-field invariant may allow assignment to a refinement-typed field when the assigned expression establishes the invariant. A const binding forbids assignment through it, including field and index writes.
Parameters#
Parameters are local bindings. Scalars are passed by value. Managed values are passed by reference, so mutating a field of a record parameter changes the caller's object.
type Counter {
n i32
}
fn bump(c Counter) {
c.n += 1
}
fn main() {
var c = Counter { n = 1 }
bump(c)
print(c.n) // 2
}
Prefer returning a new value rather than mutating a parameter unless mutation is the documented purpose of the function.
Compound Assignment#
Assignment and compound assignment are statements; they do not produce a value.
var n = 10
n += 5
n -= 3
n *= 2
n /= 4
n %= 5
var xs = [1, 2]
xs += [3]
List append with += accepts another list.
var xs = [1, 2]
xs += [3, 4]
xs += xs
++ and -- are postfix statements equivalent to += 1 and -= 1. They are not expressions.
var i = 0
i++
i--
Compound assignment on a refinement type is rejected. Build the refined value with checked construction instead.
Scope#
A declaration is visible from its declaration to the end of the enclosing block. Inner scopes may shadow outer names. A later declaration in the same scope may also shadow an earlier one.
fn main() {
const n = 10
{
const n = 20
print(n) // 20
}
print(n) // 10
}
Loop variables are scoped to the loop body. A defer captures the lexical binding visible when it was registered. See Defer.
Module Constants#
A top-level const declares a module constant. Module constants are immutable, may be declared before or after use, and may be used in contracts and type invariants. There are no mutable globals.
const LIMIT i32 = 100
const DOUBLE_LIMIT i32 = LIMIT * 2
fn below(n i32) bool => n < LIMIT
Module constants are evaluated at compile time when their initializers can be folded. A constant that depends on an impure call cannot be used where a constant expression is required.