Index a parameter tuple with a run-time index

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Jan 15 12:48:39 PST 2016


On 15.01.2016 21:42, Nordlöw wrote:
> How do I index a function parameter tuple with a run-time index?

With a switch and a static foreach:

----
void f(A...)(size_t i, A a)
{
     import std.stdio: writeln;
     switch_: switch (i)
     {
         foreach (iT, T; A)
         {
             case iT: writeln(T.stringof); break switch_;
         }
         default: writeln("??"); break;
     }
}

void main()
{
     f(0, "foo", 42); /* string */
     f(1, "foo", 42); /* int */
     f(2, "foo", 42); /* ?? */
}
----


More information about the Digitalmars-d-learn mailing list