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

Paul Backus snarwin at gmail.com
Sat Jul 16 18:59:52 UTC 2022


On Saturday, 16 July 2022 at 18:00:40 UTC, Salih Dincer wrote:
> Why can't we use this with the byte type?
>
> ```d
>   auto arr = sa!(long, [1, 2, 3]);
>   assert(is(typeof(arr) : long[3])); // ok
>
>   //auto noCompile = sa!(byte, [1, 2, 3]);
> ```

Because the literal `[1, 2, 3]` is considered an `int` array 
literal, not a `byte` array literal.

It works if you use an explicit cast:

```d
auto arr = staticArray(cast(byte[]) [1, 2, 3]);
static assert(is(typeof(arr) == byte[3])); // ok
```


More information about the Digitalmars-d mailing list