RFC-0042: struct record types over flat state slots
Status: Normative. The language gains user-defined record types
(struct): a named group of fields that, when used in a state layout, is
flattened at compile time to flat, dotted scalar state slots. It is
zero-cost sugar over the existing slot model — no new runtime representation, no
new opcodes — so it stays deterministic and byte-identical across backends.
Motivation
Position/velocity/state triplets and other records are used everywhere, but the
language only offered positional slots (s0…) and the vecN shorthand. Named
records make rules self-documenting (pos.x = pos.x + inte(vel.x)), let several
records coexist without index bookkeeping, and keep the SoA slot storage.
Declaration
world {
struct Vec3 { x = 0.0; y = 0.0; z = 0.0 }
struct Body { pos = Vec3; vel = Vec3; mass = 1.0 }
entity a { state = Body } # a record type
entity b { state = (p = Vec3, hp = 10.0) } # record + a scalar
entity c { state = (pos = (x = 7.0, y = 8.0, z = 9.0)) }# inline record
}
struct Name { field = <default>; field = <Type> }— a field is a scalar (with a default) or another declaredstruct(nested).- A
stateitem may be atype_ref(Body), a named item whose RHS is atype_ref(p = Vec3), or an inline nested record (pos = (x = 1, …)).
Rules
- Flattening. Each leaf scalar becomes one ordinary state slot, named by
its dotted path (
pos.x,pos.y,vel.z,mass). Fields are laid out in declaration order, recursively; the resulting slot indices are exactly what a hand-written flatstate = (pos.x = 0, …)would produce. - Access. Own fields are read/written as
pos.x(a dotted rule LHS or an expression name); another entity's as@a.pos.x; both are usable in rules andfuncs. The bare record name (pos) aliases its first field (pos.x), mirroringvecN. vecNalignment.vec3 pos(the built-in shorthand) yieldspos.0 … pos.2, withposaliasingpos.0; dotted struct access andvecNshare the same dotted-slot resolution.- Compile-time, zero-cost. A
structhas no runtime footprint beyond its flattened slots and is part of world state (serialized, versioned, hashed).
Diagnostics
Detail 80 covers: an unknown struct type, a struct reference cycle, and a
flattened layout exceeding the state-slot limit.
Validation
struct_record_types_layout_and_rules(lang tests): layout, dotted rules, and cross-entity@a.pos.x.cli/examples/structs.pwecompiles and runs cross-backend.- Determinism: a
struct-based program and its hand-flattened equivalent produce identical writes.
Backward compatibility
Additive: flat positional states, vecN, and named scalars are unchanged. New
grammar: struct_stmt, struct_field, type_ref, nested state_rhs, and
dot_lhs.