foreach ... else statement

BCS ao at pathlink.com
Sun Jan 4 14:11:34 PST 2009


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"?





More information about the Digitalmars-d mailing list