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

jfondren julian.fondren at gmail.com
Thu Jul 14 13:30:58 UTC 2022


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.

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.

> It should be equal to:
>
> ```D
> int[3] arr = [1, 2, 3]
> ```
>
> Also why it is GC allocated without requiring ``new``?

D doesn't have a principle like "GC allocations should all be 
signposted with `new`". These visual-signposting principles are 
neat and all, but as long as the language has pervasive 
function-calling, every single one can violate the principle. 
What D *does* have is `@nogc`

https://dlang.org/spec/garbage.html#op_involving_gc


More information about the Digitalmars-d mailing list