why static array can be reassigned with new allocation?

mw mingwu at gmail.com
Sat Oct 15 18:56:41 UTC 2022


On Saturday, 15 October 2022 at 18:44:04 UTC, mw wrote:
> Regarding the syntax problem I want to make my point clear 
> again: different semantics (variable assignment a = b v.s 
> content assignment a[] = b[]) should have different syntax, 
> *regardless* they are static or dynamic array.

Of course, with these different syntax, the compiler can do more 
for static checking:

```
   int[3] a;            // compiler know it's a static array
   a = b;               // compiler issue error: static array 
cannot be re-assigned
   a = new int[5];      // compiler issue error: static array 
cannot be re-assigned
   a = what_ever_here;  // compiler issue error: static array 
cannot be re-assigned

   a[] = b[];    // content assignment ok; but if b's size can be 
known statically at compile time and is different from 3, issue 
error at compile time; otherwise run time error.

   a[start_a .. end_b] = b[start_b .. end_b];  // same as above, 
content assignment
```



More information about the Digitalmars-d mailing list