<br><br><div class="gmail_quote">On Tue, Aug 31, 2010 at 22:41, Lutger <span dir="ltr"><<a href="mailto:lutger.blijdestijn@gmail.com">lutger.blijdestijn@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div><div></div><div class="h5"><br>
</div></div>Wait what, we have static foreach? I thought that didn't work out.<br>
<br></blockquote><div><br>foreach is automatically done at compile-time if you iterate in typetuples<br><br>string toStrings(Args...)(Args args) //btw, I regularly need this<br>{<br> string result;<br> foreach(i,Type; Args)<br>
{<br> result ~= args[i] ~ " ";<br> }<br> return result;<br>}<br><br>In the above example, I iterate in Args, getting all types and their respective indicex in turn, and use the *compile-time* index i to address the i-th value in the expression tuple args.<br>
<br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
I want this to work or something like it, is it possible?<br>
<br>
enum test = [1,2, 3];<br>
<br>
foreach(i; 0..test.length) {<br>
pragma(msg, to!string(test[i]) );<br>
}<br></blockquote><div><br>Well, 0..someEnum is not recognized by the compiler. The workaround is to create a typetuple of the correct length and use it as a support for iteration:<br><br>template ZeroTo(int to) if (to >= 0)<br>
{<br> static if (to == 0)<br> alias TypeTuple!() ZeroTo;<br> else static if (to == 1)<br> alias TypeTuple!(0) ZeroTo;<br> else<br> alias TypeTuple!(ZeroTo!(to-1), to-1) ZeroTo; // here I use the fact that integers are accepted directly as template parameters and that TypeTuple auto-flatten<br>
}<br><br>so ZeroTo!3 is (0,1,2), since I made it a range open on the right. ZeroTo!0 is empty...<br><br>enum test = [1,2, 3];<br>
<br>
foreach(i; ZeroTo!(test.length)) {<br>
pragma(msg, to!string(test[i]) );<br>
}<br><br>The from -> to version is easy, along with a step parameter.<br><br><br>Philippe<br><br></div></div>