View source code Display the source code in std/typecons.d from which this page was generated on github. Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

std.typecons.tuple - multiple declarations

Struct 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.

The choice of zero-based indexing instead of one-base indexing was motivated by the ability to use value Tuples with various compile-time loop constructs (e.g. std.typetuple.TypeTuple iteration), all of which use zero-based indexing.

Fields

Name Type Description
expand Tuple.Types Use t.expand for a Tuple t to expand it into its components. The result of expand acts as if the Tuple's components were listed as a list of values. (Ordinarily, a Tuple acts as a single value.)

Methods

Name Description
toHash Creates a hash of this Tuple.

Aliases

Name Description
fieldNames The names of the Tuple's components. Unnamed fields have empty names.
Types The types of the Tuple's components.

Templates

Name Description
opAssign Assignment from another Tuple.
opCmp Comparison for ordering.
opEquals Comparison for equality. Two Tuples are considered equal iff they fulfill the following criteria:
slice Takes a slice of this Tuple.
this Constructor taking a compatible array.
this Constructor taking a compatible Tuple. Two Tuples are compatible iff they are both of the same length, and, for each type T on the left-hand side, the corresponding type U on the right-hand side can implicitly convert to T.
toString Converts to string.

Parameters

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

Example

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

Example

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);

Example

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)));

Template tuple

Constructs a Tuple object instantiated and initialized according to the given arguments.

Arguments

template tuple(Names...);

Functions

Function name Description
tuple

Parameters

NameDescription
Names A list of strings naming each successive field of the Tuple. Each name matches up with the corresponding field given by Args. A name does not have to be provided for every field, but as the names must proceed in order, it is not possible to skip one field and name the next after it.
args Values to initialize the Tuple with. The Tuple's type will be inferred from the types of the values given.

Returns

A new Tuple with its type inferred from the arguments given.

Example

auto value = tuple(5, 6.7, "hello");
assert(value[0] == 5);
assert(value[1] == 6.7);
assert(value[2] == "hello");

// Field names can be provided.
auto entry = tuple!("index", "value")(4, "Hello");
assert(entry.index == 4);
assert(entry.value == "Hello");

Authors

Andrei Alexandrescu, Bartosz Milewski, Don Clugston, Shin Fujishiro, Kenji Hara

License

Boost License 1.0.

Comments