foreach syntax

Jonathan M Davis jmdavisProg at gmx.com
Fri Jun 29 07:57:42 PDT 2012


On Friday, June 29, 2012 12:47:28 Namespace wrote:
> A friend of mine ask me why D's foreach isn't like C#
> 
> Means, why is it like
> int[] arr = [1, 2, 3];
> 
> foreach (int val; arr) {
> 
> and not
> foreach (int val in arr) {
> 
> which it is more intuitive.
> 
> I could give him no clever answer to, so maybe someone here knows
> the reasons.

Probably because for uses ; rather than in. But it's a purely subjective 
language design decision. Walter could have used : like Java does, and it 
would have worked just as well. He decided to go with ;. On the whole, we was 
basing D on C++, not C#, so he didn't copy what C# was doing syntactically for 
anything AFAIK. Lots of stuff like this is going to vary from language to 
language, and often the decisions are arbitrary and have nothing to do with 
what other languages did or are based completely on the lanugage designer's 
personal preferences. If there's no technical reason for going with one over 
the other (and I'm not aware of one in this case), then it's up to the 
language designer to make a choice on it which is going to be completely 
subjective.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list