On Sunday, 21 January 2024 at 20:13:38 UTC, An Pham wrote:
On Saturday, 20 January 2024 at 15:59:59 UTC, Anonymouse wrote:
I remember reading this was an issue and now I ran into it
myself.
```d
import std.stdio;
void main()
{
auto names = [ "foo", "bar", "baz" ];
void delegate()[] dgs;
foreach (name; names)
{
dgs ~= () => writeln(name);
}
foreach (dg; dgs)
{
dg();
}
}
```
Expected output: `foo`, `bar`, `baz`
Actual output: `baz`, `baz`, `baz`
For c# reference when they fixed it:
https://stackoverflow.com/questions/3168375/using-the-iterator-variable-of-foreach-loop-in-a-lambda-expression-why-fails
To have a way out for old behavior by capture reference,
from: dgs ~= () => writeln(name);
to: dgs ~= () => writeln(&name);