Skip to content

BaseStruct[S @ {...}]

A read-only view of a Struct, covariant in its schema: a Struct[{a: Int}] is a BaseStruct[{a: (Int | Str)}]. A parameter that only reads a struct takes a BaseStruct, so it accepts structs with narrower items.

BaseStruct is Struct at runtime, so only a struct is a BaseStruct.

Implements: BaseIterable[Entries[S]], Index[S], Spread[S], Unpack[S, Rest: BaseMap[S], Pos: BaseSeq[S]]

Class Methods

contains target key -> Bool

Tests whether a struct has an item for a symbol or position.

let s = struct name: Alice
assert (Struct.contains s :name:)
assert (!Struct.contains(s, :port:))

Parameters

NameTypeDescription
target BaseStruct[{...}]
key (Int | Sym)

get[T @ {...}, K, D = nil] target key … -> (IndexItem[T, K] | D)

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

let s = struct name: Alice
assert_eq (Struct.get s :name:) Alice
assert_eq (Struct.get s :port: default: 22) 22

Parameters

NameTypeDescription
target BaseStruct[T]
key K
:default? D
:else? (() -> D)

Methods

(get)[K] key -> IndexItem[S, K]

Reads the item a name keys, as in s.name.

Parameters

NameTypeDescription
key K

Errors

Raises FieldError when no item has the key.

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

Returns the value at a position or key.

Parameters

NameTypeDescription
key K

Errors

Raises IndexError when no item has the key.

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

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

(spread)() -> Spread[S]

Spreads the struct's items.

(unpack)() -> BaseStruct[S]

Unpacks items by position and symbol, as a Record does.