How to create TypeTuple/ExpressionTuple from tuple/tuples

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Aug 7 10:35:14 PDT 2012


On 8/7/12, "Øivind" <oivind.loe at gmail.com> wrote:
> The last one then becomes f(v0.expand, v1.expand)

Yeah. It's somewhat possible to use a helper function for multiple
TypeTuples, but the problem is D functions cannot return language
tuples (only TypeTuple from std.typecons which is what the tuple()
call creates) so you would always need to call "expand" before the
function call, e.g.:

void f(T...)(T t)
{
    writeln(t.length);
}

auto expand(T...)(T t)
{
    static if (T.length == 1)
    {
        return t[0];
    }
    else
    static if (T.length > 1)
    {
        return tuple(t[0].expand, expand(t[1..$]).expand);
    }
}

void main()
{
    auto v0 = tuple(1, 2, 3);
    auto v1 = tuple(4, 5, 6);
    auto v2 = tuple(7, 8, 9);

    f(expand(v0, v1, v2).expand);
}

Once D is improved enough to be able to return language tuples this
should be easier to use.


More information about the Digitalmars-d-learn mailing list