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);
}
Comment #1 by ag0aep6g — 2016-08-31T18:14:51Z
Slightly reduced:
----
struct OpApply {
int opApply(int delegate(int) cb) { return 0; }
}
struct Bolinha {
int a = 0;
this(int dummy) {
OpApply moviadao;
foreach(int b; moviadao) return;
}
}
void main() {
import std.stdio;
writeln(Bolinha(0).a);
}
----
Also fails on Linux and Windows (wine).
Comment #2 by moonlightsentinel — 2021-10-26T13:19:53Z
Works reliably since 2.094.1 according to run.dlang.io / local tests
Comment #3 by robert.schadek — 2024-12-13T18:49:55Z