Skip to content

Record[...Ts]

Stores an immutable sequence of positional items and symbol-keyed items.

Records keep their items in order, and a key may repeat. A lookup finds the latest value for a key. Spreading a repeated key into a call raises UnexpectedKeyError if it names a parameter; duplicates captured by a rest remain in order. Keys are read by indexing, not as fields:

let r = record name: Alice age: 30
assert_eq $r[:name:] Alice

A variadic capture such as ...rest binds a record of the remaining arguments. Use with to replace keys in a new record:

let updated = old.with status: ready

Inherits from: Iterable[Entries[Ts]]

Implements: Index[Ts], BaseMap[Ts]

Constructor

Record source

Builds a record from one spreadable source of key-value pairs.

let r = Record {name: Alice, age: 30}

Integer keys counting up from 0 become positional items, as when spreading a dict.

The lowercase record factory instead builds a record from its arguments.

let r = record first second name: Alice

Parameters

NameTypeDescription
source Spread[Ts]

Errors

Raises TypeError for any other key that is not a symbol.

Fields

len @ Int [get]

Number of items, counting each instance of a repeated key.

Methods

(index)[K] key -> IndexItem[Ts, K]

Returns the value at a position or key.

Parameters

NameTypeDescription
key K

(iter)() -> UnpackIter[Entries[Ts]]

Iterates record items as (key, value) pairs in order. A positional item's key is its position.

(spread)() -> Spread[Ts]

Spreads the record's items.

(unpack)() -> Record[...Ts]

Unpacks record items by position and symbol.

get[D = nil] key … -> (Value | D)

Gets the value for a symbol or position, with an optional default.

Parameters

NameTypeDescription
key (Int | Sym)
:default? D
:else? (() -> D)

with **updates -> Record

Replaces keys in a new record, leaving the original unchanged.

Each updated key occupies its first position; later occurrences are removed. New keys append in the order first supplied. Repeated updates use their last value. Other items, including positional items, keep their relative order. Spread a source with ...source to supply updates.

let r = record status: pending name: Alice status: queued
assert_eq (r.with status: ready) (status: "ready", name: "Alice")

The result is typed as a plain Record, so the type checker doesn't know its keys.

Parameters

NameTypeDescription
**updates Value

Errors

Raises UnexpectedPosError for positional arguments.

without *keys -> Record

Removes all occurrences of the keys in a new record.

Missing and repeated keys are harmless. Remaining items keep their relative order, and the original is unchanged. Only symbol keys can be removed. The result is typed as a plain Record, so the type checker doesn't know its keys.

let r = record name: Alice status: ready debug: true
assert_eq (r.without :status: :debug:) (name: "Alice")

Parameters

NameTypeDescription
*keys Sym

Errors

Exception Condition
TypeError A key is not a symbol
UnexpectedKeyError A keyword argument is supplied