Delegate is left with a destroyed stack object
David Nadlinger
code at klickverbot.at
Wed Oct 30 02:56:06 PDT 2013
On Wednesday, 30 October 2013 at 09:20:40 UTC, Lionello Lunesu
wrote:
> Why? It's a struct. It should be completely fine to create a
> copy [on the heap] for the closure context
That's definitely not how D closures work, they always refer to
local variables "by reference".
One other place where this tends to crop is for code involving
loop variables, but while the behavior might be unexpected to
some, discussion has made clear that the code works as intended:
---
void main() {
import std.stdio;
void delegate()[] dgs;
foreach (i; 0 .. 5) dgs ~= { writeln(i); };
foreach (dg; dgs) dg();
}
---
If structs behaved like you want them to, the snippet would (have
to) print 0, 1, 2, 3, 4 as well, and tht's definitely too big a
language change to consider at this stage.
David
More information about the Digitalmars-d
mailing list