Wanderer:
void main() {
int[5] data;
foreach (const i; 0 .. 10)
data[i] = 0;
foreach (immutable i; 0 .. 10)
data[i] = 0;
int[10] big;
foreach (const i, x; big)
data[i] = x;
}
I'm not sure if bound checks should be removed here. Before
removal, this code gives safe runtime exception, or, as
suggested above, compile-time error.
The compiler should catch all those cases at compile-time :-)
But the compiler must recognize this as correct code:
void main() {
int[5] data;
foreach (const i; 0 .. 10)
if (i < 5)
data[i] = 0;
}
My personal opinion is that code like this should remain
inefficient, to stimulate programmers to use simpler, easier to
understand idioms, like foreach (i; data).
Java already removes several bound checks but this only increases
the array access performance, so it's not easy to see, unless you
do benchmarks (or in D compare the run-time with the run-time
with disabled array bound tests). So I don't think this compiler
improvement is going to worsen the D code you will see around :-)
Bye,
bearophile