Do we need "foreach"?
Bill Baxter
dnewsgroup at billbaxter.com
Sun Apr 27 16:55:01 PDT 2008
BCS wrote:
> Reply to Bill,
>
>> Just started wondering why we really need the foreach keyword.
>>
>> These two
>>
>> for( x ; y ; z )
>>
>> for( x ; y )
>>
>> do no seem to be ambiguous to me. You do need some lookahead, but I
>> think D has some constructs like that already.
>>
>> If we're looking to get rid of keywords it seems like a candidate. I
>> already mistakenly type "for" when I mean "foreach" fairly often.
>>
>> --bb
>>
>
> I think the syntax of for is already strange enough:
>
> int i=0;
> for(for(int j=0; j<10; i+=(j++)) if (i<0) break; i; i--) somthing();
Ok, but nobody writes code like that unless they're gunning to win an
obfuscation contest.
I would be much more willing to buy a justification like it's too easy
to forget a ';' in a for loop which would lead to strange compiler
errors. Or it's too hard for the programmer to see the difference
between two ;'s and one. That's kinda what I expect Walter's reason
was, but I was hoping to hear if that's so from someone in the know
(like Walter himself).
If we're going to have foreach expressions (as was discussed in another
thread recently), and they use a keyword unlike Python's foreach
expressions, I would much rather be able to say
auto sqr = [for(x; array) x*x];
than
auto sqr = [foreach(x; array) x*x];
Those four extra letters just seem like dead weight to me.
--bb
More information about the Digitalmars-d
mailing list