Skip to content

Struct[S @ {...}]

A mutable sequence of positional and symbol-keyed items, whose plain names are its keyed items. A struct has no methods of its own, so s.name reads the item keyed :name: and s.name = value replaces or adds it. Calling s.name args calls the item.

let s = struct host: localhost port: 22
s.port = 2222
assert_eq $s.port 2222

Each symbol key appears at most once. Positional items are read and replaced through indexing, as in s[0]. Class methods such as get take the struct as their first argument, so they can't collide with an item.

The schema S types each item, so the type checker reports reading or writing a name the schema lacks.

Inherits from: BaseStruct[S]

Implements: Assign[S]

Constructor

Struct source

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

let s = Struct {host: "localhost", port: 22}

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

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

Parameters

NameTypeDescription
source Spread[S]

Errors

Exception Condition
TypeError Another key is not a symbol
ValueError A symbol key is repeated

Methods

(assign)[K] key value

Replaces the item at a position or key. A new symbol key adds an item.

Parameters

NameTypeDescription
key K
value AssignItem[S, K]

Errors

Raises IndexError for a position with no item.

(set)[K] key value

Replaces the item a name keys, or adds one, as in s.name = value.

Parameters

NameTypeDescription
key K
value AssignItem[S, K]