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

Paulo Pinto pjmlp at progtools.org
Fri Jul 15 05:50:33 UTC 2022


On Thursday, 14 July 2022 at 14:37:34 UTC, ryuukk_ wrote:
> On Thursday, 14 July 2022 at 13:30:58 UTC, jfondren wrote:
>> On Thursday, 14 July 2022 at 13:14:59 UTC, ryuukk_ wrote:
>>> It is misleading, nobody expect this to be GC allocated
>>
>> People expect that who try to append to it later on in the 
>> function. The book "Learning D" really emphasizes stuff like 
>> this, useful but potentially surprising parts of the native 
>> types of the language.
>
> Then the compiler should tell them that it is a static array 
> and they should be explicit if they want to append
>
> Corner cases from clueless people shouldn't make the default 
> useless and misleading
>
>
>> All languages have stuff like this. D has a performance hazard 
>> in treating dynamic arrays like stacks; Go avoids that while 
>> having the fun design where appending to a slice might modify 
>> a different slice.
>
>
> I disagree hard, not "all languages have stuff like this" D has 
> stuff like this
>
> Maybe Java/C# but they don't have the concept of static arrays, 
> again, who's the target audience of D?
>

C# certainly has it and since 7.0 they can be called from safe 
contexts with Span<> types, your expectations can be fulfilled 
with

```csharp
    Span<int> arr = stackalloc int[] {1, 2, 3};
```

Godbolt link, https://godbolt.org/z/6b6MMPe7v

While Java is getting them when Valhala finally gets integrated.


More information about the Digitalmars-d mailing list