digitalmars.D - How about a "static foreach"?
- "Mehrdad" <wfunction hotmail.com> Jun 16 2012
- Andrej Mitrovic <andrej.mitrovich gmail.com> Jun 16 2012
- "Mehrdad" <wfunction hotmail.com> Jun 16 2012
- "bearophile" <bearophileHUGS lycos.com> Jun 16 2012
- Timon Gehr <timon.gehr gmx.ch> Jun 16 2012
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?
Jun 16 2012
On 6/17/12, Mehrdad <wfunction hotmail.com> wrote:This doesn't work: foreach (i; 0 .. T.tupleof.length) static if (is(typeof(T.tupleof[i]) == int)) // error!
foreach (i, type; T.tupleof) static if (is(typeof(T.tupleof[i]) == int)) But I agree, we should have a way to force a static foreach loop.
Jun 16 2012
On Sunday, 17 June 2012 at 01:09:12 UTC, Andrej Mitrovic wrote:On 6/17/12, Mehrdad <wfunction hotmail.com> wrote:This doesn't work: foreach (i; 0 .. T.tupleof.length) static if (is(typeof(T.tupleof[i]) == int)) // error!
foreach (i, type; T.tupleof) static if (is(typeof(T.tupleof[i]) == int)) But I agree, we should have a way to force a static foreach loop.
lol sorry, bad example. They aren't always so easy to fix. :-)
Jun 16 2012
Mehrdad: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?
Vote or comment, please: http://d.puremagic.com/issues/show_bug.cgi?id=4085 Bye, bearophile
Jun 16 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.
Jun 16 2012









Andrej Mitrovic <andrej.mitrovich gmail.com> 