why static array can be reassigned with new allocation?
mw
mingwu at gmail.com
Sun Oct 16 11:47:35 UTC 2022
On Sunday, 16 October 2022 at 09:04:59 UTC, Nick Treleaven wrote:
> 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.
Someone showed this code very earlier in this thread already:
void main() {
int[3] arr;
writeln(arr.ptr);
}
Output:
7FFC1A072820
>> 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.
Not in my OP example.
> A static array is a value type so assignment needs to be
> supported for generic code.
That is not excuse to mix two different semantics into one syntax.
Especially D has `static if`.
More information about the Digitalmars-d
mailing list