Foreach Closures?

Alex Rønne Petersen xtzgzorex at gmail.com
Sun Apr 8 16:29:33 PDT 2012


On 09-04-2012 01:26, Kevin Cox wrote:
> I was wondering about the foreach statement and when you implement
> opApply() for a class it is implemented using closures.  I was wondering
> if this is just how it is expressed or if it is actually syntatic
> sugar.  The reason I aski is because if you have a return statement
> inside a foreach it returns from the outside function not the "closure".
>
> I was just wondering if anyone could spill the implementation details.
>
> Thanks,
> Kevin

A lot of magic happens with opApply.

Basically, when you exit a foreach block that's using opApply, DMD 
translates this into exiting the closure and *then* doing the actual 
operation (i.e. return from the outer function).

So, they're not quite closures in the usual sense.

-- 
- Alex


More information about the Digitalmars-d mailing list