Skip to main content

Sequences and tuples

Sequences​

[T] is an ordered sequence whose elements all have type T. Write one as a bracketed list of elements; the trailing comma is optional.

let values: [Float] = [10., 20., 30.];
let trailing = [10., 20., 30.,];
let first = values[0];

Every element must have the same type, and that type is the sequence's element type.

The empty literal [] takes its element type from its surroundings — a declared type, or whatever it is combined with. Where nothing fixes it, annotate the binding:

let empty: [Float] = [];

Take sequences apart with head and tail, and prepend to one with cons.

Tuples​

(A, B) is a fixed-size tuple whose positions can have different types. As in a sequence literal, the trailing comma is optional.

let placement = (100., 200., true);
let x = placement.0;
let reflected = placement.2;

The comma is what makes a one-element tuple: (x) is just x, and (x,) is a tuple holding it. The same goes for types, where (A) is A and (A,) is a one-element tuple.

let single: (Float,) = (100.,);

The empty tuple () is the unit type.