Thread overview
Array-wise expressions and range checking
Jun 16, 2013
Alexander
Jun 16, 2013
Jonathan M Davis
Jun 16, 2013
Alexander
June 16, 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?
June 16, 2013
On Sunday, June 16, 2013 11:09:27 Alexander wrote:
> 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];
> }"

Yeah, but as you failed to set any of c's elements to anything first, and they're default initialized to NaN, and all math done on NaN results in NaN, so none of c's elements will have changed in your example.

> 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?

That does look like a compiler bug. It should throw a RangeError, because a and b have different lengths, but it isn't. Please report it:

http://d.puremagic.com/issues

- Jonathan M Davis
June 16, 2013
Opened the issue http://d.puremagic.com/issues/show_bug.cgi?id=10384

Thank you!

On Sunday, 16 June 2013 at 09:35:23 UTC, Jonathan M Davis wrote:
> On Sunday, June 16, 2013 11:09:27 Alexander wrote:
>> 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];
>> }"
>
> Yeah, but as you failed to set any of c's elements to anything first, and
> they're default initialized to NaN, and all math done on NaN results in NaN,
> so none of c's elements will have changed in your example.
>
>> 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?
>
> That does look like a compiler bug. It should throw a RangeError, because a
> and b have different lengths, but it isn't. Please report it:
>
> http://d.puremagic.com/issues
>
> - Jonathan M Davis