How to initialize static arrays with variable data

bearophile bearophileHUGS at lycos.com
Fri Feb 19 09:15:20 PST 2010


Steven Schveighoffer:

> It would be nice to allow this:
> int[3] x = [a, b, c];

This is allowed now.

Regarding dynamic arrays allocated on the stack I can see few possibilities:
1) Make the compiler smarter so it can perform escape analysis on arrays too, and not just on objects.
2) Use a specified syntax, time ago I have proposed a natural one:
scope int[] arr = new int[n];
I like this, but I think Walter plans to remove scoped classes too, so I think he doesn't love the idea of scoped dynamic arrays.
3) Allow variable length stack allocated arrays, like in C99, you can use the natural syntax:
int[n] arr;
Where n can also be a compile time variable. I like this, I think Andrei likes it enough, but I think Walter is not interested. The risk with such arrays is of a stack overflow.

Bye,
bearophile



More information about the Digitalmars-d mailing list