Index a parameter tuple with a run-time index
Meta via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Fri Jan 15 12:52:46 PST 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.
More information about the Digitalmars-d-learn
mailing list