Skip to content

Containers

Tuples, lists, sets, and mappings cover the built-in collection shapes. Ownership and freezing determine which operations are safe and which values can serve as keys.

Design documentation

This chapter describes the language design. Examples are illustrative; implementation availability and unresolved rules are explained in Design status.

Selecting a collection

Use a tuple when positions have fixed roles, a list for a runtime-sized sequence, a set for uniqueness, and a mapping to associate keys with values. Insertion order in a set or mapping is an iteration guarantee; it does not settle the still-open equality rules for those containers.

A mapping whose value type permits null needs to distinguish a present key with a null value from an absent key. The selected lookup model is Found(value) versus Missing; the final method name and result-type syntax are not yet specified.

Shapes and literals

Four built-in container shapes are selected; names remain provisional:

Shape Purpose Illustrative spelling
Tuple Fixed positions; homogeneous or heterogeneous (int, string)
Growable sequence Homogeneous runtime-sized collection list[int]
Set Unique elements set[int]
Mapping Unique keys with associated values map[string, int]

There is no separate fixed-size array type: tuples cover the fixed-size shape. Tuples can have mutable slots or be deeply frozen. Tuple shape alone does not make the contents immutable.

let values = [1, 2, 3]
let pair = (1, "hello")
let singleton = (1,)
let names = set{"alice", "bob"}
let counts = {"alice": 3, "bob": 2}

Expected types guide element literals. Empty literals require enough context for inference. {} denotes a mapping; set{} denotes a set. Exact construction and mutation syntax remain provisional.

Iteration, keys, and mutation

Container rules:

  • Mappings and sets preserve insertion order during iteration.
  • Replacing a mapping value preserves its position; removing and reinserting moves the entry to the end.
  • Keys and set elements are deeply frozen and hashable.
  • Hashes and equality depend on frozen contents, not mutable external state.
  • Frozen containers can be hashable if all relevant contents are hashable.
  • Mutable objects are allowed as mapping values.
  • Missing-key indexing raises a declared exception. A separate lookup distinguishes Found(value) from Missing, including when the stored value is null.
  • Duplicate mapping-literal keys are errors; explicit update operations replace existing values.
  • Structural mutation during active borrowed iteration is prohibited in safe code.

Protocols expose collection capabilities without requiring one concrete implementation. Homogeneous tuple notation, heterogeneous runtime indexing, slicing ownership, collection equality, and precise hash contracts remain open.