A delegate problem, create delegation in loop

lijie cpunion at gmail.com
Thu Jul 5 19:54:28 PDT 2012


On Thu, Jul 5, 2012 at 3:36 PM, bearophile <bearophileHUGS at lycos.com> wrote:

> 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();
> }
>
>
Thanks bearophile.

Best regards,

-- Li Jie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20120706/7d746b61/attachment.html>


More information about the Digitalmars-d mailing list