why static array can be reassigned with new allocation?

Nick Treleaven nick at geany.org
Sun Oct 16 09:04:59 UTC 2022


On Sunday, 16 October 2022 at 01:54:51 UTC, mw wrote:
> On Sunday, 16 October 2022 at 00:27:56 UTC, Paul Backus wrote:
>>
>> 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]);
>
> From the context of this discussion thread, "var reassign" to 
> static array means: change a.ptr to where it pointing to, i.e 
> the address. And "content assignment" means: change the content 
> of the array (but not the a.ptr, the address).

A static array doesn't have a ptr runtime field.

> You example here is "content assignment".
>
> My main point is these two different assignments shouldn't use 
> the same syntax, regardless they are static or dynamic array.

There is no problem with assigning a static array to another, 
because the lengths are always checked at compile time. A static 
array is a value type so assignment needs to be supported for 
generic code.


More information about the Digitalmars-d mailing list