Thread overview
Index a parameter tuple with a run-time index
Jan 15, 2016
Nordlöw
Jan 15, 2016
anonymous
Jan 15, 2016
Meta
Jan 15, 2016
Justin Whear
Jan 15, 2016
Meta
Jan 15, 2016
Meta
January 15, 2016
How do I index a function parameter tuple with a run-time index?
January 15, 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); /* ?? */
}
----
January 15, 2016
On Friday, 15 January 2016 at 20:42:47 UTC, Nordlöw wrote:
> How do I index a function parameter tuple with a run-time index?

I believe it's impossible because a parameter tuple is not a runtime entity. If it was an expression tuple (a compile-time tuple of only values, no types or symbols) and all of the values had a common base type, you could put it in an array and index that.
January 15, 2016
On Friday, 15 January 2016 at 20:48:39 UTC, anonymous wrote:
> 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); /* ?? */
> }
> ----

And of course I'm proven wrong as soon as I post :) Sometimes I forget how powerful D's  code generation abilities are.
January 15, 2016
On Fri, 15 Jan 2016 20:52:46 +0000, Meta wrote:

> And of course I'm proven wrong as soon as I post :) Sometimes I forget how powerful D's  code generation abilities are.

Username doesn't check out, :(
January 15, 2016
On Friday, 15 January 2016 at 21:47:21 UTC, Justin Whear wrote:
> On Fri, 15 Jan 2016 20:52:46 +0000, Meta wrote:
>
>> And of course I'm proven wrong as soon as I post :) Sometimes I forget how powerful D's  code generation abilities are.
>
> Username doesn't check out, :(

Huh?