why static array can be reassigned with new allocation?
mw
mingwu at gmail.com
Sat Oct 15 17:18:02 UTC 2022
On Saturday, 15 October 2022 at 17:09:53 UTC, rassoc wrote:
> On 10/15/22 18:20, mw via Digitalmars-d wrote:
>> 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
>> ```
> These python arrays aren't static though.
>
> Also, good old python will give you a view/slice of the whole
> array in the later case, not element-wise assignment. Numpy
> works differently and will fail if your shapes aren't the same:
>
> >>> a = np.array([1,2,3])
> >>> b = np.array([1,2,3,4,5])
> >>> a[:] = b[:]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: could not broadcast input array from shape (5,)
> into shape (3,)
>
> Not all that different from your initial static array example.
you can always specify the start, end index:
```
a[start_a: end_a] = b[start_b: end_b]
```
It will fail if the length does not match; or even you can argue
that it's also runtime error (right, Python is not static
compiled language).
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.
More information about the Digitalmars-d
mailing list