Loft protects a value from accidental changes with one word, const: mark a variable or parameter with it and the compiler refuses to compile the writes that word forbids. The check happens before the program runs, so it costs nothing while it runs, and it reaches every write — through a field, an element, a function the value is handed to.
struct Counter {
value: integer
}
The two places const can go
A name has a slot, and the slot holds a value. Those are two different things to freeze, so const has two positions and they mean opposite halves:
- 'const' before the NAME —
const x = …— freezes the slot. You can never pointxat something else, but the value it holds stays mutable: you may still append to it and write its elements and fields. This is the builder shape — set it up once, fill it in place. - 'const' before the TYPE —
x: const T— freezes the value. You may swap in a whole new value, but you can never reach into the one that is there. This is the read-only shape — a shared table, a parameter a function promises not to touch.
Write both — const x: const T — and the name is fully immutable.
A plain number or boolean has no interior separate from its slot, so for those the two positions mean the same thing: both freeze it completely.
const before the TYPE is what a function uses to promise "I will not modify this". Every write through the parameter is a compile error: appending to it, writing an element, writing a field, or writing a field of a field.
fn read_value(self: const Counter) -> integer {
self.value
}
A parameter without const can write, and the write reaches the caller.
fn increment(self: Counter) {
self.value += 1
}
fn main() {
const local variables
const before the name is the common case for a local: a configuration value or a lookup table that should never be pointed somewhere else deep inside a long function.
const limit = 100;
assert(limit == 100, "const integer is readable");
For a whole number the two positions agree, so this one is frozen outright: limit = 200 and limit += 1 are both compile errors.
A const local holding a collection is the builder shape — the slot is write-once, the contents keep growing.
const totals = [1, 2, 3];
totals += [4];
totals[0] = 9;
assert(len(totals) == 4, "a const local still appends: {len(totals)}");
assert(totals[0] == 9, "a const local still takes an element write");
The other position freezes the contents instead, and leaves the slot free.
frozen: const vector<integer> = [1, 2, 3];
frozen = [7, 8];
assert(len(frozen) == 2, "a value-const local still re-points: {len(frozen)}");
frozen += [9] would not compile: the value is read-only.
The same split applies to a struct.
const cfg = Counter {value: 42 };
assert(cfg.value == 42, "const reference is readable");
assert(read_value(cfg) == 42, "const passed to const param");
A value that must not change while some code runs is passed as a const parameter, as read_value takes it: the compiler then refuses every write in that code, and in the functions it hands the value on to, before the program runs. A copy (w = cfg) is free to change.
}
Run it yourself
This page is a loft program. It runs here, in your browser — press Run, then ask it something.
Stop on a line
// Copyright (c) 2024-2026 Jurjen Stellingwerff
// SPDX-License-Identifier: LGPL-3.0-or-later
// @NAME: Store Locks
// @TITLE: Immutability with const
//
//
// Loft protects a value from accidental changes with one word, 'const': mark a
// variable or parameter with it and the compiler refuses to compile the writes
// that word forbids. The check happens before the program runs, so it costs
// nothing while it runs, and it reaches every write — through a field, an
// element, a function the value is handed to.
struct Counter {
value: integer
}
// ## The two places 'const' can go
// A name has a slot, and the slot holds a value. Those are two different things
// to freeze, so 'const' has two positions and they mean opposite halves:
//
// * **'const' before the NAME** — `const x = …` — freezes the **slot**. You can
// never point `x` at something else, but the value it holds stays mutable:
// you may still append to it and write its elements and fields. This is the
// builder shape — set it up once, fill it in place.
// * **'const' before the TYPE** — `x: const T` — freezes the **value**. You may
// swap in a whole new value, but you can never reach into the one that is
// there. This is the read-only shape — a shared table, a parameter a function
// promises not to touch.
//
// Write both — `const x: const T` — and the name is fully immutable.
//
// A plain number or boolean has no interior separate from its slot, so for those
// the two positions mean the same thing: both freeze it completely.
// 'const' before the TYPE is what a function uses to promise "I will not modify
// this". Every write through the parameter is a compile error: appending to it,
// writing an element, writing a field, or writing a field of a field.
fn read_value(self: const Counter) -> integer {
self.value
}
// A parameter without 'const' can write, and the write reaches the caller.
fn increment(self: Counter) {
self.value += 1
}
fn main() {
// ## const local variables
// `const` before the name is the common case for a local: a configuration
// value or a lookup table that should never be pointed somewhere else deep
// inside a long function.
const limit = 100;
assert(limit == 100, "const integer is readable");
// For a whole number the two positions agree, so this one is frozen outright:
// `limit = 200` and `limit += 1` are both compile errors.
//
// A const local holding a collection is the builder shape — the slot is
// write-once, the contents keep growing.
const totals = [1, 2, 3];
totals += [4];
totals[0] = 9;
assert(len(totals) == 4, "a const local still appends: {len(totals)}");
assert(totals[0] == 9, "a const local still takes an element write");
// The other position freezes the contents instead, and leaves the slot free.
frozen: const vector<integer> = [1, 2, 3];
frozen = [7, 8];
assert(len(frozen) == 2, "a value-const local still re-points: {len(frozen)}");
// `frozen += [9]` would not compile: the value is read-only.
//
// The same split applies to a struct.
const cfg = Counter {value: 42 };
assert(cfg.value == 42, "const reference is readable");
assert(read_value(cfg) == 42, "const passed to const param");
// A value that must not change while some code runs is passed as a 'const'
// parameter, as `read_value` takes it: the compiler then refuses every write in
// that code, and in the functions it hands the value on to, before the program
// runs. A copy (`w = cfg`) is free to change.
}