DMD Bug or not? foreach over struct range
    Nick Sabalausky 
    SeeWebsiteToContactMe at semitwist.com
       
    Tue May 15 23:43:19 PDT 2012
    
    
  
This code:
------------------------------------------
import std.stdio;
struct Foo
{
    int val;
    @property bool empty() {
        return val >= 5;
    }
    @property int front() {
        return val;
    }
    void popFront() {
        val++;
    }
}
void main()
{
    Foo foo;
    foreach(val; foo)
        writeln(foo.val, " ", val);
}
------------------------------------------
Expected output:
0 0
1 1
2 2
3 3
4 4
Actual output:
0 0
0 1
0 2
0 3
0 4
It seems that foreach is iterating over an implicit copy of 'foo' instead of 
'foo' itself. Is this correct behavior, or a DMD bug?
    
    
More information about the Digitalmars-d-learn
mailing list