[Issue 16454] New: Return in the body of a foreach in a constructor backed by opApply corrupts the object
    via Digitalmars-d-bugs 
    digitalmars-d-bugs at puremagic.com
       
    Wed Aug 31 10:45:38 PDT 2016
    
    
  
https://issues.dlang.org/show_bug.cgi?id=16454
          Issue ID: 16454
           Summary: Return in the body of a foreach in a constructor
                    backed by opApply corrupts the object
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Mac OS X
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: andrepuel at gmail.com
import std.stdio;
struct OpApply {
    int opApply(int delegate(int) cb) {
        return cb(42);
    }
}
struct Bolinha {
    int a;
    this(ref OpApply moviadao) {
        foreach(int b; moviadao) {
            this.a = b;
            return;
        }
    }
};
void main() {
    import std.stdio;
    OpApply range;
    writeln(Bolinha(range).a);
}
The expected print result was 42. But I get a random memory garbage instead.
The following minor modification in main will 'fix' the issue:
void main() {
    import std.stdio;
    OpApply range;
    Bolinha littleBall = Bolinha(range);
    writeln(littleBall.a);
}
--
    
    
More information about the Digitalmars-d-bugs
mailing list