enum and tuples

Ali Çehreli acehreli at yahoo.com
Fri Aug 9 09:36:16 PDT 2013


On 08/08/2013 11:16 PM, captaindet wrote:> hi,

 > i am still struggling getting to grips with tuples, especially
 > typetuples.

Yes, especially TypeTuples. :)

 > typetuples seem to live in a shadow world

My feelings! :)

Coincidentally, I am struggling with the same question at the moment as 
I am in the process of revising and translating a Tuples chapter. I got 
stuck just yesterday but I have not re-read Philippe Sigaud's document yet.

I don't answer your questions; the following is some of what I 
understand so far:

import std.stdio;
import std.typetuple;

void foo(int i, string s, double d)
{
     writefln("foo: %s %s %s", i, s, d);
}

void main()
{
     // A TypeTuple is nothing but a combination of three separate entities,
     // loosely kept together. The following is one that is made of three
     // values:
     auto var = TypeTuple!(10, "hello", 1.5);

     // Although it looks like foo is receiving a single entity, because 
var is
     // a list of three entities (not actually a single entity of three
     // things), it matches what foo expects:
     foo(var);  // prints: "foo: 10 hello 1.5"
}

Mixing values and types in a TypeTuple changes the kind of the TypeTuple 
and it becomes something different. Let's say I want it to represent the 
need of "give me a string array of 10 elements". This can be done by a 
value and a type:

     auto var = TypeTuple!(10, string);    // compilation ERROR

Yeah, although TypeTuple!(10, string) is legal syntax, it cannot be used 
in the same way as a TypeTuple that consists entirely of values. 
However, although the alias is only for convenience, the following works:

     alias var = TypeTuple!(10, string);

In this case we know that var[1] is a type itself and var[0] is an int 
value. So, we can use them in that way:

     alias var = TypeTuple!(10, string);

     alias ElemType = var[1];         // it is the 'string' type itself
     auto length = var[0];            // it is 10
     auto arr = new ElemType[length];

     writeln(arr);    // prints ["", "", "", "", "", "", "", "", "", ""]

One difference between a TypeTuple that consists merely of values and a 
TypeTuple that consists at least one type is that the former can be used 
as a function parameter list as well as a template parameter lest. 
However, the latter can only be used as a template parameter list. (This 
is because a type cannot appear as a function argument.)

For example, when combined with compile time foreach, the template can 
test each argument separately with a static if:

void bar(T...)()
{
     foreach (arg; T) {
         static if (is (arg)) {
             // I have a type
             auto var = arg.init;
             writefln("bar has just created a %s.init.", arg.stringof);

         } else {
             // I have a value
             writefln("bar is looking at the value %s.", arg);
         }
     }
}

     alias var = TypeTuple!(10, string);
     bar!var();

/* Prints:
bar is looking at the value 10.
bar has just created a string.init.
*/

Ali



More information about the Digitalmars-d-learn mailing list