std.typecons.tuple
- multiple declarations
- Struct Tuple
- Template tuple
Struct Tuple
Tuple
of values, for example
is a record that
stores an Tuple
!(int, string)int
and a string
.
can be used to bundle
values together, notably when returning multiple values from a
function. If Tuple
obj
is a
, the individual members are
accessible with the syntax Tuple
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
s with various compile-time
loop constructs (e.g. Tuple
std.typetuple.TypeTuple
iteration), all of which use
zero-based indexing.
Fields
Name | Type | Description |
---|---|---|
expand
|
Tuple. |
Use t.expand for a t to expand it into its
components. The result of acts as if the 's components
were listed as a list of values. (Ordinarily, a acts as a
single value.)
|
Methods
Name | Description |
---|---|
toHash
|
Creates a hash of this .
|
Aliases
Name | Description |
---|---|
fieldNames
|
The names of the 's components. Unnamed fields have empty names.
|
Types
|
The types of the 's components.
|
Templates
Name | Description |
---|---|
opAssign
|
Assignment from another .
|
opCmp
|
Comparison for ordering. |
opEquals
|
Comparison for equality. Two s are considered equal
iff they fulfill the following criteria:
|
slice
|
Takes a slice of this .
|
this
|
Constructor taking a compatible array. |
this
|
Constructor taking a compatible . Two 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 .
|
toString
|
Converts to string. |
Parameters
Name | Description |
---|---|
Specs | A list of types (and optionally, member names) that the 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
members can be named. It is legal to mix named and unnamed
members. The method above is still applicable to all fields.
Tuple
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
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
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
Name | Description |
---|---|
Names | A list of strings naming each successive field of the .
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 with. The 's type will
be inferred from the types of the values given. |
Returns
A new
with its type inferred from the arguments given.
Tuple
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