Array-wise expressions and range checking

Alexander rride.a at gmail.com
Sun Jun 16 02:09:27 PDT 2013


Hello,

I'm reading "The D programming language", part "4.1.7 Array-wise 
Expressions".

It states that

"The effect of an array-wise expression is that of a loop 
assigning each element of the
left-hand side in turn with the corresponding index of the 
right-hand side. For example,
the assignment

auto a = [1.0, 2.5, 3.6];
auto b = [4.5, 5.5, 1.4];
auto c = new double[3];
c[] += 4 * a[] + b[];

is the same as

foreach (i; 0 .. c.length) {
     c[i] += 4 * a[i] + b[i];
}"


So I assume that the following code should generate runtime 
exception during evaluation of c[2] = b[2] + a[2], but it doesn't 
happen.

auto b = [1, 2];
auto a = [2, 3, 4];
int c [] = new int[3];
c[] = b[] + a[];
return c;

Is it an intended behaviour or a compiler bug?


More information about the Digitalmars-d-learn mailing list