Re: Closures over temporary variables

2022-06-14 Thread Ali Çehreli via Digitalmars-d-learn
On 6/14/22 02:04, bauss wrote: > You have to do it like this: > > ``` > dgs ~= ( (n) => () { writeln(n); })(i); > ``` The same thing with a named function as well as with iota(): import std.range; import std.algorithm; import std.stdio; void main() { void delegate()[] dgs; auto makeDg(int

Re: Closures over temporary variables

2022-06-14 Thread bauss via Digitalmars-d-learn
On Tuesday, 14 June 2022 at 08:26:53 UTC, Anonymouse wrote: What is the correct way of making this output `0 1 2`? ```d void delegate()[] dgs; foreach (immutable i; 0..3) { dgs ~= () => writeln(i); } foreach (dg; dgs) { dg(); // outputs: `2 2 2` } ``` You have to do it like this: `

Closures over temporary variables

2022-06-14 Thread Anonymouse via Digitalmars-d-learn
What is the correct way of making this output `0 1 2`? ```d void delegate()[] dgs; foreach (immutable i; 0..3) { dgs ~= () => writeln(i); } foreach (dg; dgs) { dg(); // outputs: `2 2 2` } ```