why static array can be reassigned with new allocation?
mw
mingwu at gmail.com
Sat Oct 15 16:20:51 UTC 2022
On Friday, 14 October 2022 at 19:46:06 UTC, jfondren wrote:
> On Friday, 14 October 2022 at 18:57:44 UTC, mw wrote:
>> Why this code can be compiled and causing runtime error? it
>> should be caught at compile time!
>
> ```d
> import std.stdio;
>
> void main() {
> int[3] arr;
> writeln(arr.ptr);
> arr = new int[3];
> writeln(arr.ptr);
> writeln(typeid(arr));
> }
> ```
>
> output (e.g.):
>
> ```
> 7FFD5EBF1530
> 7FFD5EBF1530
> int[3]
> ```
>
> It's not a reassignment of arr, but a copying of the contents
> of the dynamic array, and it crashes with an oversized array as
> the lengths don't match,
This is so confusing, we should have two different syntax for
this two different semantics:
```
arr = other; // assign array variable to `other`
arr[] = other[]; // assign array contents
```
I think D has the second form already, then the first form should
not be a shorthand for the second form.
These two have two different semantics.
If you view the above two statements as in Python (numpy), it's
very clear their meaning are different:
``` Python
arr = other; // assign array variable to `other`
arr[:] = other[:]; // assign array contents
```
More information about the Digitalmars-d
mailing list