On Monday, 24 February 2020 at 20:00:20 UTC, Adam D. Ruppe wrote:
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.

D'oh! I am actually familiar with the pattern from Javascript, used it many times, but somehow got it mixed up with something else and couldn't make it work.

Thanks.

Reply via email to