Structs and Records
Structs
A struct declares fields and the methods operating on them. Construction is a direct call to the free memberwise initializer: positional arguments fill fields in order, and named arguments bind by field name, so P(b: 1, a: 2) sets a to 2. There is no new keyword. Assignment copies by value.
struct Vec3 {
let x: Float;
let y: Float;
let z: Float;
}
let v = Vec3(1.0, 2.0, 3.0);
println!(v.x, v.y, v.z);Fields default to package visibility. private restricts a field to its enclosing scope; reading a private field from outside is an E203 error. Structs accept type parameters in angle brackets; parameters erase to Any in field slots, so one declaration serves every element type with a single shared layout.
struct Box<T> {
let value: T;
}
let b = Box(42);
println!(b.value);init and deinit
init(params) runs at construction when the memberwise default is insufficient: validation, derived fields, resource acquisition. deinit runs at destruction for manual teardown. .init() is never written; the construction call invokes it.
struct Meter {
let reading: Int;
init(reading: Int) {
this.reading = reading;
}
deinit {
println!("meter dropped");
}
fn bump(amount: Int): Int {
this.reading = this.reading + amount;
return this.reading;
}
}
let m = Meter(20);
println!(m.bump(22));Records
A record declares a value type in one line: positional construction, copy-by-value assignment, the same field access as struct, but no methods and no init. Records carry zero ARC overhead.
record Point(x: Float, y: Float)
let a = Point(1.0, 2.0);
let b = a;
println!(b.x, b.y);Records serve coordinates, colors, and messages: data flowing through functions without attached behavior.
Anonymous records
Anonymous records skip the declaration: { id: 1, name: "Al" } builds a record value inline with field access and destructuring. A ...rest binding collects every field not named explicitly.
let user = { id: 1, name: "Al", role: "admin" };
let { id, ...meta } = user;
println!(id, meta.name);Value-type reference
| Type | Construction | Methods | Copy | init |
|---|---|---|---|---|
struct | positional call Vec3(1.0, 2.0, 3.0) | yes, in body | by value | optional, runs at construction |
record | positional call Point(1.0, 2.0) | no | by value, zero ARC overhead | no |
| anonymous record | inline { id: 1 } | no | by value | no |
Field visibility: fields default to package visibility; private restricts a field to its enclosing scope, and reading one from outside is an E203 error.
struct Named {
let label: String;
private let serial: Int;
}
let n = Named("tag", 7);
assert(n.label == "tag", "visible");Generic structs take type parameters in angle brackets; parameters erase to Any in field slots, so one declaration serves every element type with a single shared layout. Records and anonymous records destructure by field name with : alias renames; a ...rest binding collects unnamed fields.
struct Box<T> {
let value: T;
}
let bi = Box(42);
let bs = Box("hi");
assert(bi.value == 42 && bs.value == "hi", "erased generics");Summary
struct: fields, methods, free memberwise construction,init/deinitlifecycle, generic parameters erased toAny.record: one-line positional value types with copy semantics and no methods.- Anonymous records build inline and destructure with
...restcollection.