Skip to main content

tuple

An ordered, fixed-length group of two or more values, each with its own type.

const fix: (float64, float64) = (42.3601, -71.0589);
const labeled: (string, uint32) = ("track", 7);

Elements​

A tuple type is a parenthesized list of two or more types, and a tuple value is a parenthesized list of that many expressions.

  • A tuple has at least two elements. A single parenthesized expression is a grouping, not a one-element tuple.
  • An element may be any type, a tuple included.
  • Two tuple types are assignable when they have equal arity and each element is assignable position-wise.
  • Equality compares element by element.
const nested: ((string, uint32), bit) = (("track", 7), true);
const sectors: (float32, float32)[] = [(0.0, 90.0), (90.0, 180.0)];

Access​

._N selects element N, counting from zero. N is written as a literal, so the arity is known and the selection is checked.

  • ._N is valid for N inside the tuple's arity and nowhere else.
  • A let tuple pattern destructures instead, binding one name per element.
  • A match tuple pattern does the same, and covers a tuple discriminee when each element pattern is itself irrefutable.
const fix: (float64, float64) = (42.3601, -71.0589);
const nested: ((string, uint32), bit) = (("track", 7), true);
const repeated: (float64, float64) = (42.3601, -71.0589);

fix._0            // => 42.3601
nested._0._1      // => 7
fix == repeated   // => true

Errors​

An element index the tuple does not have​

const fix: (float64, float64) = (42.3601, -71.0589);

const altitude: float64 = fix._2;   // error: invalid-member-access

A pattern that binds the wrong number of elements​

function latitude(fix: (float64, float64)) -> float64 = {
    let (lat, lon, alt) = fix;   // error: tuple-pattern-arity
    lat;
};

A tuple of a different arity​

const fix: (float64, float64, float64) = (42.3601, -71.0589, 120.0);

const flat: (float64, float64) = fix;   // error: type-mismatch
  • array — a variable-length collection of one element type.
  • struct — the same grouping with names on the parts.