@safe: import std; struct S { int delegate(char x) @safe _dlg; @safe @nogc int opApply(int delegate(char x) @safe dlg) { _dlg = dlg; return 0; } } @safe unittest { S s; @safe void f() { int i = 1; foreach(x; s) { writeln(x, ": ", i); } // BUG disappears if this is used instead: // s.opApply((char x){ writeln(x, ": ", i); return 0; }); // because then the frame of 'f' comes from GC correctly (non-scoped delegate) } f(); s._dlg('x'); writeln("Again:"); s._dlg('y'); }
If `scope` is added just before `int delegate`, the `_dlg = dlg;` will be diagnosed as an error.
The problem is solved if -preview=dip1000 is used to compile it. But as a general practice, I recommend adding `scope` to the delegate parameter to opApply().
When is dip1000 becoming default? Until then - the default behavior is buggy. Is the bug policy to reject any bug that will be solved by future features in preview mode?