How about a "static foreach"?

Timon Gehr timon.gehr at gmx.ch
Sat Jun 16 19:26:37 PDT 2012


On 06/17/2012 03:02 AM, Mehrdad wrote:
> This doesn't work:
>
> foreach (i; 0 .. T.tupleof.length)
> static if (is(typeof(T.tupleof[i]) == int)) // error!
> ...
>
> because the foreach isn't static.
>
>
> It forces me to write:
>
> template Iota(size_t i, size_t n)
> {
> static if (n == 0) { alias TypeTuple!() Iota; }
> else { alias TypeTuple!(i, Iota!(i + 1, n - 1)) Iota; }
> }
>
> foreach (i; Iota!(0, T.tupleof.length)) // "static foreach"
> static if (is(typeof(T.tupleof[i]) == int))
> ...
>
> which gets annoying quickly.
>
>
> Why not just let us write
>
> static foreach (i; 0 .. T.tupleof.length)
> static if (is(typeof(T.tupleof[i]) == int))
> ...
>
> so that the code is a lot more succinct and easy to write?

Last time I asked, the answer was that static foreach had been part of
the plan, but Walter experienced implementation problems.

I don't think there is any reason why there is no static foreach except
for the fact that someone would need to implement it.


More information about the Digitalmars-d mailing list