why static array can be reassigned with new allocation?

Paul Backus snarwin at gmail.com
Sun Oct 16 00:27:56 UTC 2022


On Saturday, 15 October 2022 at 20:05:25 UTC, mw wrote:
> While the 1st statement should just be var assignment, which 
> does NOT need to check length at all, the error message should 
> be "Error: static array cannot be re-assigned".

Static arrays can be reassigned, though:

     int[3] a = [1, 2, 3];
     int[3] b = [4, 5, 6];
     b = a;
     a[0] = 4;
     assert(b == [1, 2, 3]);

A static array is a value type, so reassigning one is the same as 
reassigning any other value type, like an int or a struct--it 
creates a copy of the data.

The actual problem is that D lets you mix static arrays (which 
are value types) and slices (which are reference types) in the 
same assignment.


More information about the Digitalmars-d mailing list