Array literals are weird.

Blatnik blatblatnik at gmail.com
Sat May 1 13:04:35 UTC 2021


On Saturday, 1 May 2021 at 12:35:00 UTC, Adam D. Ruppe wrote:
> It'd probably break a lot of code if this were to change now.

But the compiler could probably catch the mistake everywhere it 
happens with DIP 1000. And then it's just a matter of replacing 
[...] with [...].dup where you actually need it.

Although to be fair, even though the compiler (dmd) catches 
really simple cases, it doesn't catch more complex ones:

```D
int[] bug1() {
   int[3] a = [1, 2, 3];
   return a; // Error: Returning `a` escapes reference to a local 
variable.
}

int[] bug2() {
   int[3] a = [1, 2, 3];
   int[] b = a;
   return b; // No error!
}
```

But with DIP 1000 this is caught as well:

```D
int[] bug2() {
   int[3] a = [1, 2, 3];
   int[] b = a;
   return b; // Error: Scope variable `b` may not be returned.
}
```


More information about the Digitalmars-d mailing list