why static array can be reassigned with new allocation?

mw mingwu at gmail.com
Sun Oct 16 16:38:56 UTC 2022


On Sunday, 16 October 2022 at 14:19:55 UTC, Paul Backus wrote:
> On Sunday, 16 October 2022 at 11:47:35 UTC, mw wrote:
>> That is not excuse to mix two different semantics into one 
>> syntax.
>>
>> Especially D has `static if`.
>
> The exact same "semantic mixing" also occurs between classes 
> and structs:
>
>     class C
>     {
>         int n;
>         this(int n) { this.n = n; }
>     }
>
>     struct S
>     {
>         int n;
>     }
>
>     void main()
>     {
>         C c1 = new C(123);
>         C c2;
>         c2 = c1;
>         c1.n = 456;
>         assert(c2.n == 456); // "var reassignment"
>
>         S s1 = S(123);
>         S s2;
>         s2 = s1;
>         s1.n = 456;
>         assert(s2.n == 123); // "content reassignment"
>     }
>
> Should we also have two separate syntaxes for these assignments?

Not in this case, because

1) there is no confusion here about struct v.s class.
2) the array example has alternative and much clear syntax a[] = 
b[]


And, if we continue with your (value v.s reference type) example, 
and do the equivalent in my OP example:

     s1 = new S;

You got:
onlineapp.d(29): Error: cannot implicitly convert expression `new 
S(0)` of type `S*` to `S`

This error message is *exactly* what I wanted for my OP example 
for arrays!

Thank you for this example.



More information about the Digitalmars-d mailing list