Do we need "foreach"?

Nick Sabalausky a at a.a
Tue May 6 13:04:49 PDT 2008


"Robert Fraser" <fraserofthenight at gmail.com> wrote in message 
news:fv48l1$670$4 at digitalmars.com...
>
> Java's solution is to use a colon in place of the semicolon.
>
> for(x : y)

I rather like C#'s "x in y". It's more obvious than either the difference 
between two or three semicolons, or the difference between semicolon and 
normal colon. Of course, C# still uses it like "foreach(x in y)" instead of 
"for(x in y)". But in any language, I do think I'd prefer the dichotomy of 
"for(;;)" and "for(x in y)", as opposed to pretty much any other syntax. 
Especially for reverse iteration - "foreach_reverse" is incredibly long. So 
"for_reverse" would be an improvement. But in D's case, I don't know if 
using "in" in this manner could cause parsing trouble since "in" already has 
another meaning.

Assuming it's reasonably possible, I would vote for adding something like 
"for(x in y)" or "for(x from y)" or "for(x of y)", etc., as either a 
replacement or an alternative for "foreach(;)".

Speaking of "in", (and getting a bit off topic, sorry), I might as well put 
it out there that I vastly prefer C#'s behavior of requiring attributes like 
"ref" and "out" to be explicitly specified when calling a function:

void Foo(out int someVal) { someVal = 5; }

int bar;
Foo(out bar); // OK
Foo(bar); // Error, caller MUST show that they KNOW that bar may change





More information about the Digitalmars-d mailing list