www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Index a parameter tuple with a run-time index

reply =?UTF-8?B?Tm9yZGzDtnc=?= <per.nordlow gmail.com> writes:
How do I index a function parameter tuple with a run-time index?
Jan 15 2016
next sibling parent reply anonymous <anonymous example.com> writes:
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); /* ?? */ } ----
Jan 15 2016
parent reply Meta <jared771 gmail.com> writes:
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.
Jan 15 2016
parent reply Justin Whear <justin economicmodeling.com> writes:
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, :(
Jan 15 2016
parent Meta <jared771 gmail.com> writes:
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?
Jan 15 2016
prev sibling parent Meta <jared771 gmail.com> writes:
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.
Jan 15 2016