Variables
Brunost has two kinds of variable bindings: immutable (låst) and mutable (open). Prefer låst — mutability is opt-in.
Immutable variables — låst
låst name er "Astrid"
låst year er 1814låst variables cannot be reassigned after declaration. Any attempt raises an ImmutableAssignment error at runtime.
låst x er 10
x er 20 // RuntimeError: ImmutableAssignmentMutable variables — open
open counter er 0
counter er counter + 1
counter er counter + 1
// counter is now 2Use open whenever you need to update a variable over time — loop counters, accumulators, etc.
The assignment operator — er
er serves two purposes depending on context:
| Context | Meaning |
|---|---|
Declaration (låst x er …) | Bind a name to a value |
Statement (x er …) | Reassign a mutable variable |
Expression (a erSameSom b) | Equality comparison (use erSameSom instead) |
TIP
To test equality, use erSameSom, not er. Using er in an expression context assigns rather than compares.
Compound assignment operators
Mutable variables support shorthand update operators:
open score er 10
score += 5 // score is now 15
score -= 3 // score is now 12
score *= 2 // score is now 24
score /= 4 // score is now 6These are equivalent to writing score er score + 5 etc., but shorter.
Scope
Variables follow lexical (block) scoping. A variable declared inside a block is not visible outside it:
bruk terminal
viss (sant) gjer {
låst inner er "hello"
terminal.skriv(inner) // works
}
// terminal.skriv(inner) // would fail — inner is out of scopeFunctions create their own scope; they can read variables from enclosing scopes (closures).
Naming
Identifiers may contain letters (including æ, ø, å), digits, and underscores. They must not start with a digit.
låst gardsbruk er "Tømmerholt"
låst ål er 42