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

jfondren julian.fondren at gmail.com
Thu Jul 14 20:23:12 UTC 2022


On Thursday, 14 July 2022 at 19:57:24 UTC, Basile B. wrote:
> On Thursday, 14 July 2022 at 17:57:13 UTC, Ali Çehreli wrote:
>> - int[$]      // Not perfect because the array does not have a 
>> length
>
> I wanted to implement that in my language and found a subtle 
> problem that makes the implementation of the feature a not so 
> nice looking special case : the semantics of `int[$]` require 
> the initializer unlike `auto`, which can be directly replaced 
> by the initializer type. That breaks the binary and (somewhat 
> ideal) approach of either `auto` and replace or full type and 
> implicit conv of the initializer.

dmd already comes very close, though:

```d
unittest {
     int[2] arr = [1, 2, 3];
}
```

This fails to compile with "Error: mismatched array lengths, 2 
and 3". So, treat `$` in `int[$]` like it's a random number, 
which already works, but then when you get to checking the 
lengths, accept 3 as the length of the array.


More information about the Digitalmars-d mailing list