void main() { auto test = Foo(3); foreach (i; test.items) { } } struct Foo { struct Generator { int opApply(int delegate(ref long) dg) { const bak = this.foo.data; // control copy int res; for (long i=0; i < bak; i++) { assert (bak == this.foo.data, "this.foo changed?!"); res = dg(i); if (res) break; } return res; } Foo foo; } ref Generator items() // ref is important { return Generator(this); // Generator.foo = this } int data; }
Fixed DMD 2.034.