Thread overview
How about a "static foreach"?
Jun 17, 2012
Mehrdad
Jun 17, 2012
Andrej Mitrovic
Jun 17, 2012
Mehrdad
Jun 17, 2012
bearophile
Jun 17, 2012
Timon Gehr
June 17, 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?
June 17, 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.
June 17, 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. :-)
June 17, 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
June 17, 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.