Foreach Closures?
Timon Gehr
timon.gehr at gmx.ch
Tue Apr 10 03:46:39 PDT 2012
On 04/10/2012 11:33 AM, Andrej Mitrovic wrote:
> On 4/9/12, Kevin Cox<kevincox.ca at gmail.com> wrote:
>> 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 don't like this subtle thing. For example let's say a newbie were to
> implement a function called "isDirEmpty". The first hint is to try to
> walk the directory iteratively and return as soon as there's a match,
> e.g.:
>
> bool isEmptyDir(string path)
> {
> foreach (_; dirEntries(path, SpanMode.shallow))
> return false;
>
> return true;
> }
>
> isEmptyDir never returns false.
Yes it does.
> All that false statement does is break
> out of the foreach loop.
>
??? No, it returns false from isEmptyDir, unless the opApply
implementation abuses operator overloading.
More information about the Digitalmars-d
mailing list