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

Steven Schveighoffer schveiguy at gmail.com
Sun Jul 17 15:23:45 UTC 2022


On Sunday, 17 July 2022 at 09:23:23 UTC, Mike Parker wrote:

> he felt that he couldn't formulate a good case for why the 
> proposed feature was sufficiently of more benefit than 
> `staticArray` to justify the new syntax.
>

staticArray for nested arrays sucks:

```d
auto foo = [[1,2,3,4].staticArray,[5,6,7,8]].staticArray;
int[4][2] foo2 = [[1,2,3,4],[5,6,7,8]];
```

The second statement is much better, but you need the first to 
infer the length. Just specifying "infer the length" with a `[$]` 
on the type is a perfect fit.

You also have an issue with const conversion that's not as easy 
to specify. e.g.:

```d
char[74] foo = "this is a long string that I don't want to have 
to count the characters in";
```

Can't use staticArray here because the type will be 
`immutable(char)[74]`.

If I want to specify it should be char, I can't do 
`staticArray!char`, because then it doesn't infer anything.

I can't even cast it, because there's no way to cast without 
knowing the correct size.

This is a simple quality-of-life improvement. I think the 
original PR was reverted because it allowed too much inference, 
like:

`auto[$] foo = [1,2,3,4];`

I think just specifying the length is dependent on the length of 
the literal is enough benefit to justify the change. We don't 
need full inference of any static array type.

-Steve


More information about the Digitalmars-d mailing list