Index a parameter tuple with a run-time index

2016-01-15 Thread Nordlöw via Digitalmars-d-learn

How do I index a function parameter tuple with a run-time index?


Re: Index a parameter tuple with a run-time index

2016-01-15 Thread anonymous via Digitalmars-d-learn

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); /* ?? */
}



Re: Index a parameter tuple with a run-time index

2016-01-15 Thread Meta via Digitalmars-d-learn

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.


Re: Index a parameter tuple with a run-time index

2016-01-15 Thread Meta via Digitalmars-d-learn

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.


Re: Index a parameter tuple with a run-time index

2016-01-15 Thread Justin Whear via Digitalmars-d-learn
On Fri, 15 Jan 2016 20:52:46 +, 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, :(


Re: Index a parameter tuple with a run-time index

2016-01-15 Thread Meta via Digitalmars-d-learn

On Friday, 15 January 2016 at 21:47:21 UTC, Justin Whear wrote:

On Fri, 15 Jan 2016 20:52:46 +, 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?