On this page

0020. Records are value structs with a lookahead literal rule

Context

The first user-defined types need familiar syntax and predictable immutable semantics, while preserving existing block parsing and both execution backends.

Decision

Declare struct Point { x: i64, y: i64 }, with at least one uniquely named field. Construct Point { x: 1, y: 2 }: every field exactly once, any written order, evaluated left to right in that order. Read p.x; assignment to a field is E0406, with help pointing to M4.2's shallow with update.

Ident { is a literal only when the following tokens, skipping newlines, are Ident :. There is no context flag. A name-colon pair cannot start a statement, so if p == Point { x: 1 } { … } is unambiguous.

Records are nominal values, copied independently on assignment or calls. Type declarations may appear in any source order; their namespace includes builtins and is separate from functions and locals. Fields accept every implemented value type. Reject direct and indirect by-value cycles with E0410, showing the first DFS cycle in source order. Dependencies include declarations nested anywhere in type arguments; typed IDs represent links.

Every value type supports structural equality: record fields in declaration order, strings by bytes including NUL. Only i64, bool and str print; other types get E0309 with field-printing help (unit retains its existing fix). C uses flat structs and structural equality helpers (ADR-0023). Interpreter records share immutable Rc<[Value]> storage with Clone handles; overwriting a local never mutates another value and releases obsolete storage.

Alternatives considered

Consequences

Field order must survive formatting and lowering even when layout order differs. Layouts are finite and definition-before-use. Large by-value copies remain until M5.6 destination passing; no allocation or representation optimization is introduced without measurements.