www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - static foreach

reply "John S. Skogtvedt" <jss2k2 chello.no> writes:
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?");
	}
}
Nov 15 2006
parent reply Max Samuha <maxter i.com.ua> writes:
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?
Nov 15 2006
parent reply Reiner Pope <reiner.pope REMOVE.THIS.gmail.com> writes:
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?

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.
Nov 15 2006
parent Max Samuha <maxter i.com.ua> writes:
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?

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.
Nov 16 2006