why static array can be reassigned with new allocation?
Nick Treleaven
nick at geany.org
Sat Oct 15 11:30:19 UTC 2022
On Saturday, 15 October 2022 at 10:31:16 UTC, matheus wrote:
> On Friday, 14 October 2022 at 20:19:20 UTC, Ali Çehreli wrote:
>> ...
>> The compiler does not perform detailed analysis of all code.
>> Since that 5 could be a runtime expression, the compiler does
>> not check it at compile time.
>> ...
>
> Yes but in this case it's known during the compiling time
> (Literal 5), couldn't this be checked and prevented?
It would be better to error on assigning a new array expression
to a static array, there's no reason to allow that. A static
array can be assigned a single element `arr = 0` instead.
Although ideally there would be some syntactical way of making it
clear when assigning a slice to a static array, perhaps requiring
`[]` at the end unless it is already a SliceExpression or an
array literal. Then the syntax would tell you if the right hand
side had a compile-time known length not (and hence you can rely
on a compile-time error if lengths don't match).
More information about the Digitalmars-d
mailing list