On Friday, 9 August 2013 at 03:30:16 UTC, SteveGuo wrote:
I suggest that change keyword *for_each* to *for* since *for* is clear enough and
less letters, like C++11 does.

Decent suggestion, but I don't think Walter, Andrei or any of the committers want to break everyone's code just to shorten some syntax.

As a proposal, however, it's not bad, and like you say it exists in other languages:

    // Go
for key, val := range x {} // where x is an array, slice or map
    // Java
    for (int x in y) {}
    // C++11
    for (int &x : y) {}
    // Python (or use iter(), range(), xrange(), etc)
    for x in y: pass

Which are similar to D's foreach:

    foreach (key, value; x) {} // where x is a suitable range

I agree that the following would be unambiguous (whichever syntax we decide is better):

    for (key, value; x) {}
    for (key, value : x) {}

But it doesn't offer enough benefit to make a breaking change or introduce redundant syntax. Saving 4 chararters isn't enough justification.

Also, I don't particularly like for_reverse, since you can't use a traditional for-loop syntax with for_reverse:

    // would be syntax error
    for_reverse (i = 0; i < 5; i++) {}
    // but this works just fine
    foreach_reverse(i; 0 .. 5);

And D doesn't stand alone in the 'foreach' arena:

    // C#
    foreach(int x in y) {}
    // Perl
    foreach (@arr) {
        print $_;
    }
    // PHP
    foreach($set as $value) {}
    foreach ($set as $key => $value) {}
    // Scala
    some_items foreach println

I don't really see a compelling reason to switch. Then again, this is only my opinion, so I can't speak for the community.

Reply via email to