Arlen
Arlen

Reputation: 6835

Tuple vs TypeTuple in D

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

Upvotes: 6

Views: 915

Answers (3)

user541686
user541686

Reputation: 210455

A Tuple is a data type, consisting of a collection of fields that you specify.

A TypeTuple is simply "a bunch of things" that the compiler knows about at compile time; it has no existence at run time.

(Contrary to its name, a TypeTuple can hold pretty much anything -- not just types!)

Upvotes: 4

ratchet freak
ratchet freak

Reputation: 48196

Tuple is a collection of variables (like a struct) while TypeTuple is a collection of type which you can use in template checks

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 examine whether the instantiations of your templates use the correct types

Upvotes: 3

Michal Minich
Michal Minich

Reputation: 2657

tuple std.typecons normal ordinary tuple of values.

typetuple in std.typetuple is tuple of types. It can be used at compile time like in this example function, where it limits allowed types for funciton to int, long and double only.

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

Upvotes: 3

Related Questions