Records (roadmap #16)

type Vec2 = Vec2(x: Float, y: Float)
type Particle = Particle(pos: Vec2, vel: Vec2, alive: Bool)

let o = Vec2(y: 0.0, x: 0.0)                       # named arguments, any order
o.x                                                # field access
fn fall(p, dt):
  p.with(vel: p.vel.with(y: p.vel.y - 9.8 * dt))   # copy with changes: in place when unique
match s:
  Circle(r: r) => ...                              # named patterns; other fields are `_`

What exists

How it is implemented: no engine changes

  1. At parse time, named arguments and named patterns become positional (src/records.rs).
  2. After type checking, r.f and r.with(..) become ordinary match expressions, using the record type the checker resolved for each one. The interpreter, VM, native backend, bounds prover and fuzzer all just see matches. - A chain like p.pos.x becomes one match with nested patterns, so no intermediate record is copied. The exhaustiveness checker now treats single-constructor patterns as always matching. - The lowered program must type-check again; if it doesn't, that is a hard internal error. This check caught a real gap, an exhaustiveness false positive on nested patterns. Before it existed, the VM would have quietly lost type information.

Value semantics that run in place

A record update never changes what anyone else sees. Yet the particle simulation in examples/records.tr allocates only its initial objects:

pipelineallocationsreused in placerefcount overhead removed
borrowing only30,205060.5%
borrowing + drop-reuse (default)627150,000100%

That run is 200 particles × 150 steps, with each step updating the particle and two nested Vec2s. Three mechanisms make it work:

  1. Rebuilds reuse their own cell. In r.with(..), the new values are computed before the record is taken apart, as in Rust's S { f: e, ..r }. The record is therefore dead when it is rebuilt, and drop-reuse rebuilds it in the same memory.
  2. Nested updates are taken apart first. In p.with(vel: p.vel.with(...)), p is a variable that the new values use only through its fields. So p is taken apart first and p.vel reads the field directly. Both levels are then unique, and both are rebuilt in place.
  3. List elements are taken out, not copied (new take reads). With ps[i] = step(ps[i]), the old read copied the element, so it was shared and step had to allocate. Now, when nothing touches the list between the read and the write, and nothing in between can leave early, a unique list gives up the element (leaving a placeholder that the write replaces). This works in the VM and natively (rt_index_take). Shared lists still copy: let ys = xs; xs[0] = xs[0].with(...) leaves ys untouched, and that case is tested.

Speed. The new particles benchmark updates 1,000 particles for 300 steps with .with(...). It takes 4.0 ms natively, against 67.7 ms for idiomatic mutable Python dataclasses (17× faster), with identical output.

Evidence

Honest notes