Skip to content

Hash Maps

Chloë Strainge edited this page Oct 14, 2026 · 1 revision

Hash maps

A hash maps keys to values. Entries have no guaranteed visit or display order. Use [:] for an empty hash and [key: value] for entries.

let $stock: [String: Integer] = ["tea": 8, "coffee": 5]
$stock["tea"] = $stock["tea"] - 2
echo $stock["tea"] $stock["missing"] $stock.count
6 () 2

Missing keys read as (). Assignment inserts or replaces an entry. Unlike array indexes, hash keys are values rather than positions. "1", 1, and true remain different keys; equal numeric 1 and 1.0 share lookup identity.

Key contracts and snapshots

[String: Integer] constrains both sides. As a value expression it constructs an empty typed hash; [(String): (Integer)] stores Type objects instead. .key_type and .value_type expose Type values for the collection contract.

Arrays, structs, and other supported values can be keys. The hash stores a key snapshot, so changing the variable used to construct a key does not move the entry:

let $coordinate = [2, 4]
let $markers = [$coordinate: "camp"]
$coordinate[0] = 9
echo $markers[[2, 4]] $markers[$coordinate]
camp ()

Strings compare by text rather than their executable marker. Named keys retain their distinct identity. Do not use a display String as a substitute for a key of a nominal type.

Iterate pairs

let $totals: [String: Integer] = ["east": 3, "west": 7]
let $sum = 0
for ($region, $count) in $totals { $sum = $sum + $count }
echo $sum
10

Each step supplies a two-item array, which can be kept as one value or destructured. A hash value of () remains data inside that pair. Iteration uses a private snapshot; writes to the source do not change pending entries. Use .properties to inspect the hash itself, rather than confusing its interface with the entries it contains.


Related: Arrays · Iteration · Reflection

Clone this wiki locally