auto arr = [1, 2, 3] should be a static array, not a GC allocated array

Salih Dincer salihdb at hotmail.com
Thu Jul 14 16:38:41 UTC 2022


On Thursday, 14 July 2022 at 13:14:59 UTC, ryuukk_ wrote:
> It is misleading, nobody expect this to be GC allocated
>
> It should be equal to:
>
> ```D
> int[3] arr = [1, 2, 3]
> ```
>
> Also why it is GC allocated without requiring ``new``?

`auto` is like a politician speaking with big promises. Unless 
you explicitly state it, you have to accept its choice. For 
example:

```d
void main()
{
   alias type = int[3];
   auto arr = cast(type)[1, 2, 3];
   assert(is(typeof(arr) : type));
}
```
It usually chooses int for numbers, its arrays are dynamic.

SDB at 79


More information about the Digitalmars-d mailing list