Lambda capture by value
Adam D. Ruppe
destructionator at gmail.com
Mon Feb 24 20:00:20 UTC 2020
On Monday, 24 February 2020 at 19:50:23 UTC, JN wrote:
> foreach (i; iota(5))
> {
> printers[i] = () { write(i); };
I know it looks silly but if you make that:
printers[i] = (int i) { return () { write(i); }; }(i);
it will do what you want.
This is something that used to be common in javascript, write a
little function that passes the capture-by-value args and returns
the lambda you actually want and call it immediately.
That extra layer causes the compiler to create a new copy of the
capture variables snapshotted in time.
More information about the Digitalmars-d-learn
mailing list