Store Locks

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:

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.

}