Lambda capture by value

kinke noone at nowhere.com
Mon Feb 24 20:05:31 UTC 2020


On Monday, 24 February 2020 at 19:50:23 UTC, JN wrote:
>     foreach (i; iota(5))
>     {
>         printers[i] = () { write(i); };
>     }

This allocates 1 closure and generates 1 lambda, so all printers 
are identical delegates. You could use a static foreach:

NumberPrinter[] printers;
static foreach (i; 0..5)
     printers ~= () { write(i); };
foreach (d; printers)
         d();


More information about the Digitalmars-d-learn mailing list