www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How about a "static foreach"?

reply "Mehrdad" <wfunction hotmail.com> writes:
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
next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
prev sibling next sibling parent "Mehrdad" <wfunction hotmail.com> writes:
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
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
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
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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