why static array can be reassigned with new allocation?

mw mingwu at gmail.com
Sat Oct 15 18:44:04 UTC 2022


On Saturday, 15 October 2022 at 18:22:10 UTC, rassoc wrote:
> On 10/15/22 19:18, mw via Digitalmars-d wrote:
>> But the main point I want to make is: these two different 
>> semantics should have different syntax, do not overload as it 
>> only confuse the programmer.
>> 
>
> They are not the same thing and behave differently for dynamic 
> and static arrays. Furthermore, there's a distinction between 
> dynamic arrays and slices: ownership of data. [1]
>
> But what you are probably looking for is the follow which is 
> behaving the same as in python:
>
> import std.stdio : writeln;
> void main() {
>     auto a = [1,2,3];
>     auto b = [1,2,3,4,5];
>     a = b.dup;
>     writeln(a is b, " ", a == b); // false true
> }
>
> [1] https://dlang.org/articles/d-array-article.html


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.

As more fun with your example:

      a = b.dup;  // so this is content assignment?
      writeln(a is b, " ", a == b); // false true

      a = b;      // this is var assignment?
      writeln(a is b, " ", a == b); // true true

This current syntax in D is as clear as mud.



More information about the Digitalmars-d mailing list