Merge tuples

Philippe Sigaud philippe.sigaud at gmail.com
Thu May 31 14:07:32 PDT 2012


On Thu, May 31, 2012 at 7:14 PM, simendsjo <simendsjo at gmail.com> wrote:
> On Thu, 31 May 2012 18:42:20 +0200, bearophile <bearophileHUGS at lycos.com>
> wrote:
>
>> simendsjo:
>>
>>> int a;
>>> string b;
>>> ToTuple!(a, b) should create Tuple!(int, "a", string, "b")
>>>
>>> template ToTuple(Variables...) {
>>>  ?
>>> }

Is the following what you're looking for?


import std.stdio;
import std.typecons;
import std.typetuple;

template TypeAndName(Variables...)
{
    static if (Variables.length == 0)
        alias TypeTuple!() TypeAndName;
    else
        alias TypeTuple!(typeof(Variables[0]), Variables[0].stringof,
TypeAndName!(Variables[1..$])) TypeAndName;
}

Tuple!(TypeAndName!(Variables)) toTuple(Variables...)() @property
{
    return Tuple!(TypeAndName!(Variables))(Variables);
}

void main()
{
    int i = 1;
    double d = 3.14;
    auto t = toTuple!(i,d);
    writeln(t.i);
    writeln(t.d);
}


More information about the Digitalmars-d-learn mailing list