why static array can be reassigned with new allocation?

Nick Treleaven nick at geany.org
Sat Oct 15 11:37:12 UTC 2022


On Friday, 14 October 2022 at 20:19:20 UTC, Ali Çehreli wrote:
> On 10/14/22 11:57, mw wrote:
>
> >      arr = new int[5];
>
> That syntax is confusing. Although it looks like a static 
> array, it's a dynamic array of length 5.

I recently updated the docs to prefer the `new E[](length)` form 
instead:

> It is preferred to use the Type(ArgumentList) form when 
> allocating dynamic arrays instead, as it is more general.

> Note: It is not possible to allocate a static array directly 
> with new (only by using a type alias).

https://dlang.org/spec/expression.html#NewExpression

At some point the `T[length]` form should be deprecated. Perhaps 
we could have a compiler switch `-strict` that deprecates a set 
of things, e.g. struct postblit. This would be used for anything 
which needs a long deprecation period and to avoid annoying 
people by adding new deprecations spread out over a number of 
releases. Instead they can update their code using the `-strict` 
check once in a while.


More information about the Digitalmars-d mailing list