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

Salih Dincer salihdb at hotmail.com
Sat Jul 16 17:12:26 UTC 2022


On Saturday, 16 July 2022 at 16:27:06 UTC, Dukc wrote:
> TDPL book deals with this question. Andrei said that 
> "experience with pascal" shows it's better to be dynamic by 
> default.
>
>> Also why it is GC allocated without requiring ``new``?

Matter turns elsewhere... Are GC-controlled D arrays better, or 
static arrays recommended by traditional programmers?

On the other hand, the last example also works with static array 
because it is being copied:

```d
void main()
{
   //...

   auto foo = () => cast(int[3])[1, 2, 3];

   arr1 = foo();
   foreach(ref el; arr1) el *= 2;
   assert(arr1 == [2, 4, 6]);

   arr2 = foo();
   foreach(ref el; arr2) el *= 5;
   assert(arr2 == [5, 10, 15]);
}
```
SDB at 79


More information about the Digitalmars-d mailing list