Maybe we don't need foreach ;-)

Bill Baxter dnewsgroup at billbaxter.com
Mon Oct 30 23:47:38 PST 2006


Walter Bright wrote:
> Bill Baxter wrote:
>> But yeh, I'm sure you can cook up some example where you change one 
>> comma to a semicolon and both are legal.
> 
>     for (a, b; c)
>     for (a; b; c)
> 
> Not only do they look very, very similar, it is not at all obvious which 
> one was intended.

Assuming you mean literally a b and c, it's pretty clear it's got to be 
a foreach that was intended.  There's no reason to stick a variable in 
either the first or last clause of a standard for.

Here's a example where the intent really isn't obvious:

    for (int a; b;)
    for (int a; b)

Anyway, given that
* I myself have been guilty of using ',' where I meant ';' in for loops,
* 'in' can't be used in place of ';' due to syntactical ambiguities,

I agree that it's safer and better to have foreach separate.

--bb



More information about the Digitalmars-d mailing list