Skip to content

Records

Construct and use anonymous structural product types.

Updated View as Markdown

An anonymous record is a product type identified by its fields rather than by a declaration name.

fn position(): { x: int, y: int } {
    return { x: 10, y: 20 }
}

Record types and values use named fields.

Structural identity

Two anonymous record types with the same field names and types are the same structural type, regardless of source field order.

first: { x: int, y: int } = { x: 1, y: 2 }
second: { y: int, x: int } = first

Changing a field name or field type creates a different record shape.

name_only: { name: string } = { name: "Atoll" }

This differs from a nominal struct, whose declaration identity is part of the type.

Literals

Name each field with name: value.

response := {
    status: 200,
    body: "ok",
}

Commas make the record shape unambiguous when a brace expression could otherwise be parsed as a block. Field shorthand is available when a visible binding has the same name.

status := 200
body := "ok"
response := { status, body, }

The checker validates each literal against an expected record type or infers a new structural shape from the fields.

Access

Fields use ordinary member syntax.

code := response.status

Access is checked structurally. A field missing from the record is a compile error.

Mutation still requires a mutable place.

mut point := { x: 1, y: 2, }
point.x = 3

Patterns

Record patterns destructure by field name.

{ status, body } := response

Rename a binding with an explicit subpattern, and use .. when the source shape may contain fields the pattern does not need.

{ status: code, .. } := response

Functions

Anonymous records are convenient for local helpers with a small one-use result.

fn bounds(values: []int): { minimum: int, maximum: int } {
    // ...
}

Prefer a named struct when:

  • the shape is exported widely;
  • methods or trait implementations belong to it;
  • its identity carries domain meaning;
  • documentation needs a stable type name;
  • recursive structure is required.

Queries

Integrated SQL projections synthesize anonymous record types.

rows := FROM Booking
    SELECT {
        id
        total: passengers * price
    }?

The resulting element shape has id and total. A function binding projection can name such a compiler-generated type elsewhere without manually duplicating the record.

See Projections and Query Projections.

Behavior

Anonymous records provide field structure, not a declaration site for inherent methods. Use generic functions constrained by structural use, or promote the shape to a named struct when behavior becomes part of its API.

Navigation

Type to search…

↑↓ navigate↵ selectEsc close