ava/tuples.md

1.3 KiB

Tuples

Tuples are heterogeneous, fixed-size, collections of values. Tuples contain 0 to N values, where N is unbounded. Tuples are similar to Records, but records have named parameters.

Syntax

let x := ()
let y := ("foo")
let z := ("foo", 1)
let w := ("foo", 1, true, 3.14)
let a: Int32 := unwrap(1)

Unwrapping a tuple of size one is just the identity function:

given A
fn unwrap: (a: A) => A
    a
end fn

Type of a Standard Tuple

Consider the tuple ("foo", 1, true, 3.14). It has type (String, Int32, Boolean, Float64).

The Empty Tuple

The type () has a single possible value, (). This is the empty tuple. It is typically used as a token to indicate side-effects with no other useful output.

Accessing Tuple Members

Each tuple member is indexed and can be directly accessed via a property of that index:

let w := ("foo", 1, true)
let x := w._1
let y := w._2
let z := w._3

Destructuring Tuples

Tuples can be destructured via pattern matching capabilities. This can take two possible forms.

Destructured Binding

Tuples may be destructured at the point of binding.

let w := ("foo", 1, true)
let (x, y, z) := w

Destructured Match Case

let w := ("foo", 1, true)

let z :=
    match w
        case ("foo", _, x) => x
        case _ => false