On 09/05/2011 10:43 PM, zeljkog wrote:
On 05.09.2011 21:55, Timon Gehr wrote:

static foreach is part of the design. The only reason it is not in the
compiler (and we have the kludgy foreach that works on tuples) is
because Walter experienced implementation difficulties.


Before implementation somebody must precisely define static foreach,
better to say static looping construct.

It works just like foreach, except that it does not introduce a new scope and can be used at module and aggregate scope.

Eg:

static foreach(x;0..5) {
    mixin("T!x var"~to!string(x)~";");
}

is equivalent to

T!0 var0;
T!1 var1;
T!2 var2;
T!3 var3;
T!4 var4;

The aggregate to loop over is evaluated at compile time:

int[] getAggregate(){return [1,2,3,];}
static foreach(x;getAggregate()) {
    pragma(msg,x);
}

writes during compilation:
1
2
3





Reply via email to