Array-wise assignment on unallocated array

Walter Bright newshound2 at digitalmars.com
Tue Jul 27 11:17:15 PDT 2010


Andrej Mitrovic wrote:
> 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?

Yes, it should throw an out of bounds error.


More information about the Digitalmars-d mailing list