foreach ... else statement
Denis Koroskin
2korden at gmail.com
Sun Jan 4 15:01:51 PST 2009
On Mon, 05 Jan 2009 01:11:34 +0300, BCS <ao at pathlink.com> wrote:
> Reply to Nick,
>
>> I had never heard of that before, but it certainly seems like
>> something I'd find useful. Any time I'm using for or foreach to find
>> something, I end up having to use some sort of "isFound" flag and
>> check that after the loop. A for...else would be much nicer, for
>> example:
>>
>
> [...]
>
>> // vs:
>> foreach(char[] key, Foo f; fooAA)
>> {
>> if(f.someFlag && key in barAA && f.someVal > barAA[key].someVal)
>> break; // found
>> }
>> else
>> Stdout.formatln("Missing!");
>
> Wait, that's not the way I would expect else to work. You (and and it
> would seem python) would have the else clause run if the loop is
> terminated by the for/foreach rater than a break/goto. I would expect
> that the else clause would be executed if the loop is terminated before
> the first iteration.
>
> I think your expectation would be a good feature but I can't really see
> else being the right keyword to use. How about "finally"? Or another
> option with scope(*) like "fallthrough"?
>
>
Same here, I expected the for/else pair to work similar to if/else, i.e. only one of the branches to be executed.
Perhaps I could get used to the syntax, though.
More information about the Digitalmars-d
mailing list