View source code
Display the source code in std/typecons.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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.

std.typecons.Tuple/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.

struct Tuple(Specs...)
  
if (distinctFieldNames!Specs);

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. AliasSeq iteration), all of which use zero-based indexing.

Constructors

NameDescription
this Constructor taking one value for each field.
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.

Fields

NameTypeDescription
expand Tuple.TypesUse 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.)

Properties

NameTypeDescription
slice[get] inout(Tuple!(sliceSpecs!(from,to)))Takes a slice by-reference of this Tuple.

Methods

NameDescription
opAssign Assignment from another Tuple.
opBinary Concatenate Tuples. Tuple concatenation is only allowed if all named fields are distinct (no named field of this tuple occurs in t and no named field of t occurs in this tuple).
opBinaryRight Concatenate Tuples. Tuple concatenation is only allowed if all named fields are distinct (no named field of this tuple occurs in t and no named field of t occurs in this tuple).
opCmp Comparison for ordering.
opEquals Comparison for equality. Two Tuples are considered equal iff they fulfill the following criteria:
rename Renames the elements of a Tuple.
rename Overload of rename that takes an associative array translate as a template parameter, where the keys are either the names or indices of the members to be changed and the new names are the corresponding values. Every key in translate must be the name of a member of the tuple. The same rules for empty strings apply as for the variadic template overload of rename.
toHash Creates a hash of this Tuple.

Aliases

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

Templates

NameDescription
toString

See Also

tuple.

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";
writeln(e[1]); // "Hello"
writeln(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)));

Example

Use tuples as ranges

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

Example

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)));
writeln(t[1]); // "2"
writeln(t[2]); // 42
writeln(t[3]); // true

Template tuple

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

template tuple(Names...) ;

Contained Functions

NameDescription
tuple

Parameters

NameDescription
Names An optional 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.

Example

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

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

Authors

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

License

Boost License 1.0.