Records
Product types, field access, construction, invariants, mutation, generics, and methods.
Contents
A record is a nominal product type with named fields.
Declaring a Record#
A declaration lists fields with a type after each name. Commas are optional.
type Point {
x i32
y i32
}
type Account {
id i32
balance i64
}
A record may be generic.
type Pair[A, B] {
first A
second B
}
type Box[T] {
value T
}
Records cannot inherit and there is no structural subtyping. Reuse comes from composition, generics, and traits.
Constructing a Record#
A literal names each field. All fields are required; duplicates are rejected and order does not matter. A trailing comma is accepted.
const origin = Point { x = 0, y = 0 }
const account = Account { balance = 100, id = 1 }
Generic records require explicit type arguments.
const pair = Pair[i32, str] { first = 1, second = "one" }
const box = Box[i32] { value = 10 }
A record value is heap-allocated and managed. Assignment copies the reference.
Field Access#
fn distance_squared(a Point, b Point) i32 {
const dx = a.x - b.x
const dy = a.y - b.y
return dx * dx + dy * dy
}
There is no record destructuring and no structural equality. Compare fields or implement Eq. To update a record, construct a new one.
fn moved(point Point, dx i32, dy i32) Point =>
Point { x = point.x + dx, y = point.y + dy }
Invariants#
A record invariant appears after the type name and before the opening brace. Field names are in scope; self is not introduced for records. Multiple invariants use repeated : invariant clauses.
pure fn ordered(lo i32, hi i32) bool => lo <= hi
type Window : invariant ordered(lo, hi) {
lo i32
hi i32
}
An invariant must be boolean and may call only pure functions. It is checked at construction. A record with an invariant cannot have one field assigned in isolation.
type Range : invariant lo <= hi {
lo i32
hi i32
}
fn widen(range Range, by i32) Result[Range, str] {
const lo = range.lo - by
const hi = range.hi + by
return Success(Range { lo = lo, hi = hi }?)
}
Range { ... }? is required because the bounds are runtime values. A violation returns Error("invariant failed for 'Range'"). See Checked Construction.
Construction and Constant Checking#
Normal construction has type T and must never fail. It is accepted only when every field is a compile-time constant satisfying the invariants, or when the value already has the target type. A constant that provably violates an invariant is a compile error.
type Positive = i32 : invariant self > 0
type Pair : invariant x == y && y > 10 {
x Positive
y Positive
}
const good = Pair { x = 12, y = 12 } // checked at compile time
// const bad = Pair { x = 5, y = 5 } // error: y > 10 fails
For runtime values, use checked construction in a Result-returning function.
fn build_pair(n i32) Result[Pair, str] {
const x = Positive { n }?
const y = Positive { n }?
return Success(Pair { x = x, y = y }?)
}
Mutation#
A field may be assigned when the binding is a var, the record has no own invariants, and the assignment does not violate a field's refinement type.
type Counter {
n i32
}
fn bump(counter Counter) {
counter.n += 1
}
fn main() {
var counter = Counter { n = 1 }
bump(counter)
print(counter.n) // 2
}
A record parameter is a reference to the caller's object.
Methods#
A record body contains no method declarations. Methods are free functions whose first parameter is the record, or trait methods implemented for the record.
fn area(rect Rect) f64 => rect.width * rect.height
fn main() {
const rect = Rect { width = 3.0, height = 4.0 }
print(rect.area())
}
trait Describable {
fn describe(self Self) str
}
impl Account of Describable {
fn describe(self Account) str => $"account {self.id}: {self.balance}"
}
See Traits.
Aliasing#
A const binding prevents assignment through that binding but does not make the object deeply immutable if another mutable reference exists.
var first = Point { x = 1, y = 2 }
var second = first
second.x = 9
print(first.x) // 9
Contracts#
pure fn solvent(account Account) bool =>
account.balance >= 0 && account.balance <= MAX_BALANCE
fn deposit(account Account, amount i64) Account
: pre amount > 0
: post r => r.id == account.id
: post r => r.balance == account.balance + amount
=> Account { id = account.id, balance = account.balance + amount }
There is no old expression. A postcondition sees the current value of a parameter, including any mutation performed by the function.
Summary#
| Feature | Rule |
|---|---|
| Declaration | type Name { field Type ... } |
| Construction | Name { field = value, ... }, all fields required |
| Generics | type Name[T] { ... }, construct as Name[T] { ... } |
| Field access | value.field |
| Field assignment | Only through var, and only when the record has no own invariants |
| Invariants | type Name : invariant expr { ... }, fields in scope |
| Methods | Free functions or trait implementations |
| Equality | No structural == |
| Patterns | No record patterns |
| Memory | Managed reference |