On this page

0315. Deep Path with Lenses and var Nested Field Assignment

Context

Tenon records are immutable values: with updates only top-level fields, and assigning to a record field (s.x = v) is rejected with E0406. Updating nested record state in TUI App models, agent transitions, and composite state therefore requires nested with boilerplate such as s = s with { a: s.a with { m: s.a.m.set(k, v) } }.

ADR-0278 already proves last-use uniqueness across arbitrary-depth nested with chains (s with { a: s.a with { m: s.a.m.set(…) } }) and rewrites container updates to in-place *SetUnique operations without copying.

Decision

  1. Deep Path with Lenses: Allow dotted field paths and compound field updates inside with update expressions (e.g., s with { pane.scroll: 0 }, s with { stats.hits += 1 }, or s with { a.m: s.a.m.set(k, v) }). Deep path updates desugar in the frontend into nested shallow with expressions, grouping sibling paths under shared prefixes, evaluating non-trivial bases once (while keeping pure local bases alias-free for ADR-0278), and evaluating replacement expressions left-to-right. Duplicate or overlapping path updates (such as updating both a and a.b, or a.x twice) are rejected deterministically with E0415.
  2. var Nested Field Assignment: Allow direct and compound field assignment statements (s.x = rhs, s.a.m = rhs, s.a.b += 1) when the root target s is a mutable var local. The frontend desugars s.p1.p2 = rhs directly into s = s with { p1.p2: rhs } (and compound updates into the corresponding nested with update, with no alias local), preserving immutable value semantics, snapshot safety, and left-to-right evaluation. Field assignment on immutable let bindings or parameters emits E0204; field assignment on non-local targets (such as f().x = 1) emits E0406.
  3. Zero-Copy Reuse via ADR-0278: Because both deep with lenses and var nested field assignments desugar strictly in the frontend to the exact alias-free nested with AST/IR shapes handled by ADR-0278's field-take liveness analysis, patterns such as s.a.m = s.a.m.set(k, v) and s = s with { a.m: s.a.m.set(k, v) } lower to zero-copy *SetUnique (copies: 0 under --report=copies) with no regions, IR, or runtime changes.

Consequences