Thread overview
static foreach
Nov 15, 2006
John S. Skogtvedt
OT: static foreach
Nov 15, 2006
Max Samuha
Nov 15, 2006
Reiner Pope
Nov 16, 2006
Max Samuha
November 15, 2006
A kind of "static foreach" already works for tuples,
why not other constants as well?

Example:

import std.typetuple;

void main() {
	alias TypeTuple!(void, void, void) TL;
	foreach (t; TL) {
		pragma(msg, "this prints three times");
	}

	const char[] s = "abc";

	foreach (c; s) {
		pragma(msg, "and maybe this should as well?");
	}
}
November 15, 2006
On Wed, 15 Nov 2006 11:05:02 +0100, "John S. Skogtvedt" <jss2k2@chello.no> wrote:

Why 'foreach' and not 'static foreach' is used for compile time iteration unlike 'static if' for compile time condition evaluation? Or is 'static' necessary in 'static if' at all? Can't the compiler determine whether 'if' is static based on the compile time evaluability of the expression?
November 15, 2006
Max Samuha wrote:
> On Wed, 15 Nov 2006 11:05:02 +0100, "John S. Skogtvedt"
> <jss2k2@chello.no> wrote:
> 
> Why 'foreach' and not 'static foreach' is used for compile time
> iteration unlike 'static if' for compile time condition evaluation? Or
> is 'static' necessary in 'static if' at all? Can't the compiler
> determine whether 'if' is static based on the compile time
> evaluability of the expression?
The important difference between 'static if' and 'if' is that 'static if' doesn't create a new scope, whereas 'if' does. The compile-time vs runtime dichotomy is just a consequence of that; deferring handling of different scopes to runtime is just too complex.
November 16, 2006
On Thu, 16 Nov 2006 09:50:35 +1100, Reiner Pope <reiner.pope@REMOVE.THIS.gmail.com> wrote:

>Max Samuha wrote:
>> On Wed, 15 Nov 2006 11:05:02 +0100, "John S. Skogtvedt" <jss2k2@chello.no> wrote:
>> 
>> Why 'foreach' and not 'static foreach' is used for compile time iteration unlike 'static if' for compile time condition evaluation? Or is 'static' necessary in 'static if' at all? Can't the compiler determine whether 'if' is static based on the compile time evaluability of the expression?
>The important difference between 'static if' and 'if' is that 'static if' doesn't create a new scope, whereas 'if' does. The compile-time vs runtime dichotomy is just a consequence of that; deferring handling of different scopes to runtime is just too complex.

Thanks for the explanation. I need to dig deeper into the specs.