English 中文(简体)
D Programming - Tuples
  • 时间:2024-12-22

D Programming - Tuples


Previous Page Next Page  

Tuples are used for combining multiple values as a single object. Tuples contains a sequence of elements. The elements can be types, expressions, or apases. The number and elements of a tuple are fixed at compile time and they cannot be changed at run time.

Tuples have characteristics of both structs and arrays. The tuple elements can be of different types pke structs. The elements can be accessed via indexing pke arrays. They are implemented as a pbrary feature by the Tuple template from the std.typecons module. Tuple makes use of TypeTuple from the std.typetuple module for some of its operations.

Tuple Using tuple()

Tuples can be constructed by the function tuple(). The members of a tuple are accessed by index values. An example is shown below.

Example

import std.stdio; 
import std.typecons; 
 
void main() { 
   auto myTuple = tuple(1, "Tuts"); 
   writeln(myTuple); 
   writeln(myTuple[0]); 
   writeln(myTuple[1]); 
}

When the above code is compiled and executed, it produces the following result −

Tuple!(int, string)(1, "Tuts") 
1 
Tuts

Tuple using Tuple Template

Tuple can also be constructed directly by the Tuple template instead of the tuple() function. The type and the name of each member are specified as two consecutive template parameters. It is possible to access the members by properties when created using templates.

import std.stdio; 
import std.typecons; 

void main() { 
   auto myTuple = Tuple!(int, "id",string, "value")(1, "Tuts"); 
   writeln(myTuple);  
   
   writeln("by index 0 : ", myTuple[0]); 
   writeln("by .id : ", myTuple.id); 
   
   writeln("by index 1 : ", myTuple[1]); 
   writeln("by .value ", myTuple.value); 
}

When the above code is compiled and executed, it produces the following result

Tuple!(int, "id", string, "value")(1, "Tuts") 
by index 0 : 1 
by .id : 1 
by index 1 : Tuts 
by .value Tuts

Expanding Property and Function Params

The members of Tuple can be expanded either by the .expand property or by spcing. This expanded/spced value can be passed as function argument pst. An example is shown below.

Example

import std.stdio; 
import std.typecons;
 
void method1(int a, string b, float c, char d) { 
   writeln("method 1 ",a,"	",b,"	",c,"	",d); 
}
 
void method2(int a, float b, char c) { 
   writeln("method 2 ",a,"	",b,"	",c); 
}
 
void main() { 
   auto myTuple = tuple(5, "my string", 3.3,  r ); 
   
   writeln("method1 call 1"); 
   method1(myTuple[]); 
   
   writeln("method1 call 2"); 
   method1(myTuple.expand); 
   
   writeln("method2 call 1"); 
   method2(myTuple[0], myTuple[$-2..$]); 
} 

When the above code is compiled and executed, it produces the following result −

method1 call 1 
method 1 5 my string 3.3 r
method1 call 2 
method 1 5 my string 3.3 r 
method2 call 1 
method 2 5 3.3 r 

TypeTuple

TypeTuple is defined in the std.typetuple module. A comma-separated pst of values and types. A simple example using TypeTuple is given below. TypeTuple is used to create argument pst, template pst, and array pteral pst.

import std.stdio; 
import std.typecons; 
import std.typetuple; 
 
apas TypeTuple!(int, long) TL;  

void method1(int a, string b, float c, char d) { 
   writeln("method 1 ",a,"	",b,"	",c,"	",d); 
} 

void method2(TL tl) { 
   writeln(tl[0],"	", tl[1] ); 
} 
 
void main() { 
   auto arguments = TypeTuple!(5, "my string", 3.3, r );  
   method1(arguments); 
   method2(5, 6L);  
}

When the above code is compiled and executed, it produces the following result −

method 1 5 my string 3.3 r 
5     6
Advertisements