Tuple vs TypeTuple in D

What is the difference between Tuple and TypeTuple? I looked at the sample code in the library, but they look similar. How can I decide what to use? and is there a good reason why Tuple is in std.typecons but TypeTuple is in std.typetuple?

+6
source share
3 answers

A Tuple is a data type that consists of a set of fields that you specify.

A TypeTuple is just a bunch of things that the compiler knows about at compile time; it does not exist at run time.

(Unlike its name, TypeTuple can contain almost everything, not just types!)

+4
source

tuple std.typecons normal normal set of values.

typetuple in std.typetuple is a type tuple. It can be used at compile time, as in this example function, where it limits the valid types for funciton to only int, long and double.

 void foo (T) (T arg) { static assert staticIndexOf!(T,TypeTuple!(int, long, double)) != -1, T.stringof ~" is not allowed as argument for foo"); } 
+3
source

Tuple is a set of variables (e.g., struct), and TypeTuple is a set of types that you can use when validating a template

 Tuple!(int,"index",real,"value") var; 

defines a variable with var.index a int and var.value a real

a TypeTuple is used when you want to check if your template instances are using the correct types

+3
source

Source: https://habr.com/ru/post/888563/


All Articles