Foreach, return not exist the function.

Ali Çehreli via Digitalmars-d digitalmars-d at puremagic.com
Sun Jan 11 15:50:17 PST 2015


On 01/11/2015 12:25 PM, Zaher Dirkey wrote:

 > reproduce example here
 > http://dpaste.dzfl.pl/13fb453d0b1e

That link doesn't work for me. (?)

Does opApply return the delegate's return value ('b' below)?

import std.stdio;

struct S
{
     int opApply(int delegate(int) dg)
     {
         foreach (i; 0 .. 10) {
             int b = dg(i);
             if (b) {
                 writefln("Exiting opApply with %s", b);
                 return b;
             }
         }

         return 0;
     }
}

int foo()
{
     auto s = S();
     foreach (i; s) {
         writeln(i);
         return 42;
     }

     return 0;
}

void main()
{
     assert(foo() == 42);
}

Ali



More information about the Digitalmars-d mailing list