Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Tuples

A tuple is a collection of values, each of which can be of different type. Typically, each value is paired with an identifier, allowing a tuple to function similarly to a key-value store.

test

use std::debug::assert_eq;

tuple = (width = 10cm, depth = 10cm, volume = 1l);

assert_eq([tuple.width, 10cm]);
assert_eq([tuple.depth, 10cm]);
assert_eq([tuple.volume, 1l]);

assert_eq([tuple, (width = 10cm, depth = 10cm, volume = 1l)]);

Partially Named Tuples

A tuple may lack identifiers for some or even all of its values. In such cases, these unnamed values within the tuple must all be of different types.

test

(10cm, 10cm², 1l);

Otherwise, they would be indistinguishable since the values in a tuple do not adhere to a specific order.

Arbitrary Order

The order of values have no consequences for equality.

test

// these tuples are equal
std::debug::assert_eq([(1l, 10cm, 10cm²), (10cm, 10cm², 1l)]);

Arbitrary Units

Different units of values have no consequences for equality.

test

// these tuples are equal
std::debug::assert_eq([(1000cm3, 100mm, 0.01m²), (10cm, 100cm², 1l)]);

Ambiguous Elements

Either names or types must be unique in a tuple.

test

(10cm, 10mm, 1m); // error: ambiguous type Length