Delegates: Print 0..9

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Dec 1 12:12:15 PST 2016


On 12/01/2016 11:28 AM, unDEFER wrote:
 > Hello!
 > Simple using of delegates:
 >
 > ===========
 > #!/usr/bin/rdmd
 > import std.stdio;
 >
 > void main()
 > {
 >     void delegate() functions[];
 >
 >     foreach (i; 0..10)
 >     {
 >         void print()
 >         {
 >             writefln("%s", i);
 >         }
 >
 >         functions ~= &print;
 >     }
 >
 >     foreach (i; 0..10)
 >     {
 >         functions[i]();
 >     }
 > }
 > =============
 >
 > Prints
 > $ ./delegates.d
 > 9
 > 9
 > 9
 > 9
 > 9
 > 9
 > 9
 > 9
 > 9
 > 9
 >
 > How to print 0..9?

This is a common issue with D and some other languages (as I had learned 
during a Dart language presentation, of which Dart does not suffer 
from). All those delegates do close on the same loop variable. You need 
to produce copies of the variable.

First, the scary syntax that produces a lambda from an int:

import std.stdio;

void main()
{
     // Note: dmd's -de command line switch warned me about the use of
     // C-style syntax, so I moved the brackets after the type name:
     void delegate()[] functions;

     foreach (i; 0..10)
     {
         void print(int j)
         {
             writefln("%s", j);
         }

         functions ~= ((a) => (() => print(a)))(i); // <-- SCARY
     }

     foreach (i; 0..10)
     {
         functions[i]();
     }
}

Better:

         // Returns a lambda
         auto makePrinter(int j) {
             return {              // <-- returns a lambda
                 writefln("%s", j);
             };
         }

         functions ~= makePrinter(i);

But of course there are better ways of doing things e.g. producing 
numbers. Here is one with D's ranges:

import std.stdio: writeln;
import std.range: iota;
import std.algorithm: each;

void main() {
     10.iota.each!writeln;
}

Ali



More information about the Digitalmars-d-learn mailing list