Hashes
Mutable key-value maps. Type is hash.
Syntax
echo
user: hash = { name: "Echo", active: true };
name: str = user["name"];
user["active"] = false;Example
echo
user: hash = { name: "Ada", active: true };
user["role"] = "admin";
say(user);
say(user.keys());text
{"name": "Ada", "active": true, "role": "admin"}
["name", "active", "role"]Notes
- Literal keys: identifiers or string literals. Bare identifiers become string keys.
- Runtime indexing requires a string key.
ensure()sets a default if missing; returns the current value either way. Common for counters and buckets.take()andtake_last()mutate the hash.mapValues(f)returns a new hash with the same keys.filter(f)keeps entries wheref(value)istrue.- Iteration order is insertion order;
mapValues/filterpreserve it. - A hash bound with
constis frozen; field assignment and mutating methods abort. - Destructuring:
{ id: int, name: str } = user;. Missing keys abort. Extra keys are ignored (unless the type isexact).
Common Mistakes
- Non-string keys at runtime
- Expecting a missing key to return
null(it aborts) - Forgetting that
take()removes the key
Current Limitation
- Runtime indexing: string keys only.
- Key and value types are not generic.
