How can I put the current value of a variable into a delegate?

Nick Treleaven nick at geany.org
Mon May 6 15:54:11 UTC 2024


On Monday, 6 May 2024 at 06:29:49 UTC, Liam McGillivray wrote:
> Here's a line that caused a bug that took me awhile to find:
> ```
> foreach(card; unitCards) card.submitted = delegate() => 
> selectUnit(card.unit);
> ```

I think you can do:
```d
     import std.algorithm.iteration : each;
     unitCards.each!(c => c.submitted = () => selectUnit(c.unit));
```



More information about the Digitalmars-d-learn mailing list