Array-wise assignment on unallocated array

Andrej Mitrovic andrej.mitrovich at gmail.com
Tue Jul 27 10:26:51 PDT 2010


There's an example in TDPL showing array-wise expressions. I've modified the code and removed the allocation. This will compile and run without exceptions:

void main() {
    auto a = [0.5, -0.5, 1.5, 2];
    auto b = [3.5, 5.5, 4.5, -1];
    
    double[] c;
    c[] = (a[] + b[]) / 2;
}

I think assignments to unallocated arrays should throw a runtime exception, or at least give out a compile warning. Compare this to this code which throws a RangeError:

import std.stdio;

void main() {
    double[] c;
    c[0] = 4;
}

And the equivalent array-wise assignment which doesn't throw exceptions:

import std.stdio;

void main() {
    double[] c;
    c[] = 4;
}

Bugzilla-worthy?


More information about the Digitalmars-d mailing list