D Tuples - D Programming Language

What are D Tuples?

Tuples are used for combining multiple values as a single object. Tuples contain a sequence of elements. The elements may be types, expressions, or aliases. The number and elements of a tuple are fixed at compile time and they can't be modified at run time.

Tuples have characteristics of each struct and arrays. The tuple elements can be of different types like structs. The elements may be accessed thru indexing like arrays. They’re implemented as a library feature by the Tuple template from the std.typecons module. Tuple uses 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.

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

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.

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

Expand Property and Function params

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

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

TypeTuple

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

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

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

D Programming Language Topics