[Issue 1245] New: static foreach shouldn't define new scope and introduce new variables

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed May 23 13:38:00 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1245

           Summary: static foreach shouldn't define new scope and introduce
                    new variables
           Product: D
           Version: 1.014
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: baryluk at mpi.int.pl


import std.stdio;

void Code(x...)() {
        foreach (i, xi; x) {
                static if (xi != 0) { // dosn't works, see below
              //static if (x[i] != 0) { // workaround
                        writefln(xi);
                }
        }
}


void main() {
        alias Code!(1, 6, 0, 2, 5) c;
        c();
}

// staticforeachif.d(5): Error: expression xi != 0 is not constant 
// or does not evaluate to a bool

with workaround, it works, and display:
1
6
2
5
as needed

Imho foreach over tupple shouldn't introduce new scope and variables xi,
but as static if be in the same scope (if needed you always can add { .. }
and xi (second parameter in foreach), should be actually alias to x[i]

I'm using this in optimalisation of general code generators.


-- 



More information about the Digitalmars-d-bugs mailing list