Skip to content
Chloë Strainge edited this page Oct 15, 2026 · 2 revisions

Arrays

An array keeps ordered values. Indexes start at zero. Without an annotation its items can have different types; an annotation supplies an element contract.

let $stops: [String] = ["harbor", "museum", "garden", "station"]
let $afternoon = $stops[1..3]
echo $afternoon...
echo $stops[..=1]...
museum garden
harbor museum

.. excludes the end and ..= includes it. Omitted bounds use the array edges. Bounds must be nonnegative and in range; an exclusive end can equal the length. Reversed bounds produce an empty slice after bounds are checked.

Value copies and writes

let $original = [[1, 2]]
let $copy = $original
$copy[0][0] = 9
echo $original[0]...
echo $copy[0]...
1 2
9 2

Indexed writes replace existing positions. They do not grow the array. Reading a slice makes a copy; assigning through a slice is not supported.

Spread with ... to insert elements into another literal or supply arguments. An unexpanded array remains one function argument:

let $middle = [2, 3]
let $all = [1, $middle..., 4]
echo $all...
echo $all.reverse...
1 2 3 4
4 3 2 1

Element types and shapes

[String] means a homogeneous array type in annotations. As a value expression, a single bare type entry also creates a typed empty array. [(String)] instead stores the String type object. Multiple entries are ordinary values.

[Integer, String] in an annotation describes exactly two positions. [Integer,] is a one-position shape; [Integer] allows any length. Fixed shapes are useful for destructuring and iterator results.

.value_type returns a Type value. Explicit annotations are retained on empty arrays; otherwise homogeneous contents infer a type and mixed/unknown contents use any.

Useful methods

  • Native: count, sort, reverse, zip, next_item, properties.
  • Prelude: empty, sum, min, max, unique, append, prepend, push, push_many, pop, peek.

Numeric sort uses the exact mixed-number comparison used by <, <=, >, and >=.

reverse, sort, append, and prepend return new arrays. Stack methods mutate $self; push adds at the front, push_many pushes in supplied order, and pop removes the front item. min, max, pop, and peek can return () when empty. zip stops at the shorter input. Sorting accepts mutually comparable items and does not execute references stored as String values.


Related: Iteration · Methods · Pipelines

Clone this wiki locally