www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Static loops

reply "Tofu Ninja" <emmons0 purdue.edu> writes:
To me it seems odd that we have a static if but no static while 
or static for or static foreach.

Basicly all a static loop would do is unroll itself at compile 
time. The only reason I want this is that currently it is 
impossible to use a loop variable in any sort of complietime 
expression such as a static if or a template instantiation.

Was/is something like this planned? Is there a way to get similar 
functionality now? So far the only way I have found to insert 
loop variables into a compile time expression is to use a side 
function to do looping and generate a string for mixins, which is 
very very very ugly, am I missing somthing?


I saw one thread from 2004 about the same thing with very little 
replies...
http://forum.dlang.org/thread/c7hjcc$2hph$1 digitaldaemon.com

[Edit]
Ok before I posted this, I did a few more tests and it seems like 
foreach has this capability somewhat, which is just confusing. 
Using foreach on a tuple causes it to be evaluated at compile 
time, which is nice but confusing. Come to think of it, I don't 
know how you would loop over tuples otherwise...
Feb 15 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Tofu Ninja:

 To me it seems odd that we have a static if but no static while 
 or static for or static foreach.
See: https://d.puremagic.com/issues/show_bug.cgi?id=4085 Bye, bearophile
Feb 15 2014
parent reply "Tofu Ninja" <emmons0 purdue.edu> writes:
On Saturday, 15 February 2014 at 09:09:59 UTC, bearophile wrote:
 Tofu Ninja:

 To me it seems odd that we have a static if but no static 
 while or static for or static foreach.
See: https://d.puremagic.com/issues/show_bug.cgi?id=4085 Bye, bearophile
I am glad to see that I am not the only one who is thinking about this. I feel like if foreach on a tuple required static in front of it, it would be much more obvious what was happening. I don't really use tuples that often so when I saw foreach on tuples, it didn't really make sense to me.
Feb 15 2014
parent Timon Gehr <timon.gehr gmx.ch> writes:
On 02/15/2014 12:20 PM, Tofu Ninja wrote:
 ...

 I am glad to see that I am not the only one who is thinking about this.
 I feel like if foreach on a tuple required static in front of it, it
 would be much more obvious what was happening.  ...
`static foreach' should not introduce a new scope for its body. (but foreach over a tuple should.)
Feb 15 2014