Erroneous usage of variable capture in loops

2017-06-02 Thread FreeSlave via Digitalmars-d
As known this code will not work as we may expect: import std.stdio; void main(string[] args) { void delegate ()[] delegates; foreach(i; 0..10) { int j = i; delegates ~= delegate() { writeln(j); }; } foreach(dlgt; delegates) { dlgt();

Re: Erroneous usage of variable capture in loops

2017-06-02 Thread ketmar via Digitalmars-d
FreeSlave wrote: As known this code will not work as we may expect: import std.stdio; void main(string[] args) { void delegate ()[] delegates; foreach(i; 0..10) { int j = i; delegates ~= delegate() { writeln(j); }; } foreach(dlgt; del

Re: Erroneous usage of variable capture in loops

2017-06-02 Thread Timon Gehr via Digitalmars-d
On 02.06.2017 16:03, FreeSlave wrote: As known this code will not work as we may expect: import std.stdio; void main(string[] args) { void delegate ()[] delegates; foreach(i; 0..10) { int j = i; delegates ~= delegate() { writeln(j); }; }