Tuple

Tuple of values, for example Tuple!(int, string) is a record that stores an int and a string. Tuple can be used to bundle values together, notably when returning multiple values from a function. If obj is a Tuple, the individual members are accessible with the syntax obj[0] for the first field, obj[1] for the second, and so on.

template Tuple (
Specs...
) if (
distinctFieldNames!(Specs)
) {
enum injectNamedFields;
}

Members

Structs

Tuple
struct Tuple
Undocumented in source.

Parameters

Specs

A list of types (and optionally, member names) that the Tuple contains.

Examples

Tuple!(int, int) point;
// assign coordinates
point[0] = 5;
point[1] = 6;
// read coordinates
auto x = point[0];
auto y = point[1];

Tuple members can be named. It is legal to mix named and unnamed members. The method above is still applicable to all fields.

alias Entry = Tuple!(int, "index", string, "value");
Entry e;
e.index = 4;
e.value = "Hello";
assert(e[1] == "Hello");
assert(e[0] == 4);

A Tuple with named fields is a distinct type from a Tuple with unnamed fields, i.e. each naming imparts a separate type for the Tuple. Two Tuples differing in naming only are still distinct, even though they might have the same structure.

Tuple!(int, "x", int, "y") point1;
Tuple!(int, int) point2;
assert(!is(typeof(point1) == typeof(point2)));

Use tuples as ranges

import std.algorithm.iteration : sum;
import std.range : only;
auto t = tuple(1, 2);
assert(t.expand.only.sum == 3);

Concatenate tuples

import std.meta : AliasSeq;
auto t = tuple(1, "2") ~ tuple(ushort(42), true);
static assert(is(t.Types == AliasSeq!(int, string, ushort, bool)));
assert(t[1] == "2");
assert(t[2] == 42);
assert(t[3] == true);

See Also

Meta