A delegate problem, create delegation in loop

bearophile bearophileHUGS at lycos.com
Thu Jul 5 00:36:16 PDT 2012


lijie:

>> import std.stdio;
>> void main() {
>>     void delegate()[] functions;
>>     foreach (i; 0 .. 5) {
>>         functions ~= {
>>             printf("%d\n", i);
>>         };
>
>     }
>>     foreach (func; functions) {
>>         func();
>>     }
>> }


import std.stdio;

void main() {
     void delegate()[] functions;

     foreach (i; 0 .. 5)
         functions ~= ((int j) => { printf("%d\n", j); })(i);

     foreach (func; functions)
         func();
}

Bye,
bearophile


More information about the Digitalmars-d mailing list