On Sunday, June 10, 2012 01:02:40 Timon Gehr wrote:
> On 06/10/2012 12:34 AM, Jonathan M Davis wrote:
> > On Sunday, June 10, 2012 00:15:01 Timon Gehr wrote:
> >> D static array literals don't perform a costly heap allocation. It is
> >> simply a bug in the implementation. This is not a compelling reason to
> >> add new syntax.
> > 
> > D
> 
> DMD
> 
> > doesn't _have_ static array literals. It only has dynamic array literals.
> > 
> > int[5] a = [1, 2, 3, 4, 5];
> 
> This is a static array literal.
> 
> > should definitely be optimized such that it doesn't do any heap
> > allocations, but the array literal itself is dynamic regardless, as
> > typeof indicates.
> I don't see a typeof there. The array literal is a static array literal
> if it is assigned/converted to a static array.

Just do

writlen(typeof([1, 2, 3, 4, 5]).stringof);

It'll be int[], not int[5]. I don't believe that there is _any_ case where an 
array literal is considered static. The closest that you get is that it will 
implicitly conert them to static arrays in statements such as

int[5] a = [1, 2, 3, 4, 5];

but as evidenced by the extra heap allocation, it still creates the dynamic 
array before doing the conversion, and that exact same assignment works if the 
dynamic array isn't a literal.

int[] a = [1, 2, 3, 4, 5];
int[5] b = a;

I think that it's quite clear that the type system always considers array 
literals to be dynamic, which is why we're getting that extra heap allocation, 
and why in no case are they inferred to be static.

> This is not about optimization. Allocating is simply incorrect. It is a
> 'wrong code' bug.

As far as I can tell, there's nothing wrong about it as far as the type system 
is concerned. Certainly, the compiler considers array literals to be dynamic, 
and that's why we're getting these issues. This behavior is _not_ desirable 
from the standpoint of efficiency, but as for as the type system goes, I don't 
see anything incorrect about it.

- Jonathan M Davis

Reply via email to