Closures over temporary variables

Anonymouse zorael at gmail.com
Tue Jun 14 08:26:53 UTC 2022


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`
}
```


More information about the Digitalmars-d-learn mailing list