Array literals are of type T[] , that is, they are dynamic arrays that are not aware of their size at compile time.
Your code compiles for the same reason as compilation:
void foo(int[] xs) { int[3] ys = xs; ... }
The compiler just doesn't know how big xs .
In your case, the compiler can know at compile time, because all the information is there, but it will be higher and further than what the compiler should do. By interpreting the code strictly, there is no type mismatch, so it compiles.
Another side effect of array literals, which are dynamic arrays, is that the code you have will actually allocate memory. It allocates a dynamic array on the heap, copies it to a static array, and then you have to wait for the garbage collection cycle before the memory is recovered. This can be the cause of poor performance if you initialize such arrays in a compressed loop.
Again, the compiler can avoid the allocation, but DMD is at least not in the current version (2.060).
Peter Alexander
source share