Capturing a variable by value?

Ali Çehreli via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Feb 3 15:55:06 PST 2016


On 02/03/2016 10:09 AM, ZombineDev wrote:

 > I think these two links, more or less, answer my question:
 > 
http://stackoverflow.com/questions/29759419/closures-in-loops-capturing-by-reference 

 >
 > https://issues.dlang.org/show_bug.cgi?id=2043

I find it more readable to give a name like makeClosure():

     foreach (i; 0 .. 10) {
         auto makeClosure = (int a) {
             return { write(a, " "); };
         };

         arr ~= makeClosure(i);
     }

Of course, makeClosure() can be moved out of the loop if needed.

Ali



More information about the Digitalmars-d-learn mailing list