Steven Schveighoffer wrote:
immutable(double)[] - The compiler stores a copy of this array somewhere in ROM and initializes the stack variable with the immutable pointer to the data.


And what about

void foo(int x) {
   auto a = [1, x, 2];

?

Should it create an immutable array on the heap? And if the user happens to need a mutable one, he has to dup it? (Causing one unnecessary memory allocation.) (Wait, is .dup even enough to get a mutable array, or will it return just another immutable one?)

Anyway, returning an array allocated on the stack is unsafe. If someone wants it, he should write:

int[3] a = [1, x, 2];

Now the only problem is, that right now, array initializers only work for static variables. Which is very stupid. Currently, this code creates an array literal, and copies it into the static array a (which is very very stupid), and if you add a "static" in front of the variable a, the thing beyond "=" is interpreted as array initializer (and x must be const).

Ideally, the line of code above would not cause a heap allocation.

Reply via email to