Delegate is left with a destroyed stack object

Max Samukha maxsamukha at gmail.com
Wed Oct 30 05:17:28 PDT 2013


On Wednesday, 30 October 2013 at 09:56:08 UTC, David Nadlinger 
wrote:

> 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

So D managed to mess up closures, too. And that's after years of 
countless complaints about the same issue in JS!





More information about the Digitalmars-d mailing list