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

jfondren julian.fondren at gmail.com
Thu Jul 14 16:50:40 UTC 2022


On Thursday, 14 July 2022 at 15:26:44 UTC, Dave P. wrote:
> Similar syntax in C is for a static array of inferred length:
>
> ```C
> int arr[] = {1,2,3};
> ```

ehh, isn't that a C99 feature? I'm still waiting for compilers to 
support it.

D has support for the older way of needing a redundant length, 
and D errors out if the length is wrong in most cases. Still, I 
also wanted inferred length with a feature like `int[_] arr`.

There are little conveniences like this where D's losing out 
against the state out of the art in 'older' languages: C got 
inferred length, C has named-member struct literals that D 
doesn't like, C++ has structured bindings.

>
> D has no way to express a similar concept without being 
> horribly verbose with `.staticArray`.

Also:

```d
enum nums = [1, 2, 3];
int[nums.length] arr = nums;
```

I'd never do exactly that, but keeping n array in compile-time 
and only statically picking from it is an option, with `@nogc` 
protecting you from accidentally building it.


More information about the Digitalmars-d mailing list