Lambda capture by value

JN 666total at wp.pl
Mon Feb 24 19:50:23 UTC 2020


import std.range;
import std.stdio;

alias NumberPrinter = void delegate();

NumberPrinter[int] printers;

void main()
{
     foreach (i; iota(5))
     {
         printers[i] = () { write(i); };
     }

     foreach (i; iota(5))
     {
         printers[i]();
     }
}

This prints 4 4 4 4 4.

How to make it so that it prints 0 1 2 3 4? Is it possible 
without changing the delegate definition to void delegate(int)?


More information about the Digitalmars-d-learn mailing list