<p><br>
On Apr 8, 2012 7:49 PM, "Timon Gehr" <<a href="mailto:timon.gehr@gmx.ch">timon.gehr@gmx.ch</a>> wrote:<br>
><br>
> On 04/09/2012 01:26 AM, Kevin Cox wrote:<br>
>><br>
>> I was wondering about the foreach statement and when you implement<br>
>> opApply() for a class it is implemented using closures.  I was wondering<br>
>> if this is just how it is expressed or if it is actually syntatic<br>
>> sugar.  The reason I aski is because if you have a return statement<br>
>> inside a foreach it returns from the outside function not the "closure".<br>
>><br>
>> I was just wondering if anyone could spill the implementation details.<br>
>><br>
>> Thanks,<br>
>> Kevin<br>
><br>
><br>
> Since opApply has to hand through the return code if it is non-zero, I assume that DMD simply generates a custom exit code for each possible way the foreach body can be exit from.<br>
><br>
> eg:<br>
><br>
> start:<br>
> foreach(x; foo){<br>
>    if(x==1) break;<br>
>    else if(x==2) return 10;<br>
>    else if(x==3) goto start;<br>
>    else if(x==4) continue;<br>
>    ...<br>
> }<br>
><br>
> ==><br>
><br>
> int __result;<br>
> start:<br>
> switch(foo.opApply((x){<br>
>    if(x==1) return 1;<br>
>    else if(x==2){__result = 10; return 2;}<br>
>    else if(x==3) return 3;<br>
>    else if(x==4) return 0;<br>
>    ...<br>
> }){<br>
>    case 0, 1: break;<br>
>    case 2: return __result;<br>
>    case 3: goto start;<br>
> }</p>
<p>Cool, so it basically translates break and continue to returns and returns to black magic.  Cool.</p>