Records & Enums
Immutable named records, dynamic map-style records, and tagged unions.
1. Records as product values
Records are literal field bundles:
person = { name: "Ada", born: 1906 }
print(person.name)
print(person["born"])
Field order is ignored for structural equality.
2. Updates are immutable by default
p = { x: 1, y: 2 }
p2 = set(p, "x", 99)
print(p, p2)
set returns a new record. The original p is unchanged.
3. Records as maps
The same {} shape is used as a dynamic map with string keys.
mut counts = {}
counts = set(counts, "cat", 1)
counts = set(counts, "cat", get_or(counts, "cat", 0) + 1)
print(get(counts, "cat"))
print(has(counts, "dog"), has(counts, "cat"))
print(keys(counts), values(counts))
Map APIs:
get(m, key)set(m, key, value)get_or(m, key, default)has(m, key)del(m, key)keys(m)values(m)
These are available in Stage-0 and documented in REFERENCE.md.
4. Map operations are immutable
set/del return new maps, so update loops normally rebind:
mut m = {}
for w in split("a b a", " "):
m = set(m, w, get_or(m, w, 0) + 1)
print(m)
5. Mutable collections
When mutability is required, Stage-0 has:
dict()for mutable string-keyed maps withdict_set,dict_get,dict_len, etc.array()/array_fromfor mutable growable sequences.
d = dict()
dict_set(d, "mode", "fast")
print(dict_get(d, "mode"), dict_len(d))
arr = array_from([1, 2])
array_push(arr, 3)
print(to_list(arr))
6. Enums (tagged unions)
type declarations provide sum types / tagged unions.
type Option:
Some(v)
None
type Result:
Ok(v)
Err(e)
Match arms destructure constructor fields.