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);
Constructors
Name | Description |
---|---|
this
|
Constructor taking one value for each field. |
this
|
Constructor taking a compatible array. |
this
|
Constructor taking a compatible Tuple . Two Tuple s 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
Name | Type | Description |
---|---|---|
expand
|
Tuple | Use t 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
Name | Type | Description |
---|---|---|
slice [get]
|
inout(Tuple!(sliceSpecs!(from,to))) | Takes a slice by-reference of this Tuple .
|
Methods
Name | Description |
---|---|
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 Tuple s 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 .
|
toString
|
Converts to string. |
toString
|
Formats Tuple with either %s , %(inner%) or %(inner%|sep%) .
|
Aliases
Name | Description |
---|---|
fieldNames
|
The names of the Tuple 's components. Unnamed fields have empty names.
|
Types
|
The types of the Tuple 's components.
|
See Also
Parameters
Name | Description |
---|---|
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
Tuple
s 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
Name | Description |
---|---|
tuple |
Parameters
Name | Description |
---|---|
Names | An optional list of strings naming each successive field of the Tuple
or a list of types that the elements are being casted to.
For a list of names,
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.
For a list of types,
there must be exactly as many types as parameters. |
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