Language Policy

Collections & Comprehensions

Canonical grammar, semantics, constraints, and deferred boundaries for Collections & Comprehensions.

Arrays, maps, sets, literals, comprehensions, spread, indexing, membership, mutation, keyability, duplicate handling, and iteration order are specified as observable collection policy.

Policy

Array<T> is the ordered sequence, Map<K,V> the ordered key/value collection, and Set<T> the ordered uniqueness collection. Array literal spread accepts Array only. Contextual set { ... } and map { key: value } literals evaluate operands left to right. Comprehensions start with for, accept ordered later for/if clauses without commas, and finalize a private accumulator only after every reachable body succeeds.

Specified behavior

Arrays preserve production order. Equal set values collapse into the first insertion slot. A dynamically duplicate map key faults without overwrite after operand evaluation; simple repeated literal keys are static errors. Set/map keys must be recursively keyable and preserve nominal identity when frozen. Removal deletes the order slot, reinsertion appends, and updating an existing map value keeps key order. in works for arrays, sets, ranges, and map.keys, not a Map value itself.

Constraints and loud decline

Mutation requires a mutable binding. Direct array indexing with a negative or out-of-range index faults; non-faulting reads use get. [T], x in map, non-keyable keys, set/map spread, iterable spread, a leading comprehension if, comma-separated clauses, bare control targeting a comprehension, and a refutable-pattern mismatch treated as filtering are rejected. Empty set/map literals require an expected type.

Deferred boundary

General user-defined iteration, lazy or parallel comprehensions, generators, tuple/iterable spread, alternate duplicate policies, and unordered collection semantics are outside the contract. Strings are not directly iterable; scalar iteration is an explicit standard-library operation.

Worked example

TOPAZ
let squares = [ for value in [1, 2, 3] => value * value ]
let ids = set { for value in [2, 1, 2] => value }
let labels = map { for value in [1, 2] => value: "item {value}" }

print("{squares}:{ids.toArray()}:{labels.keys}")