static array literal syntax request: auto x=[1,2,3]S;

Jonathan M Davis jmdavisProg at gmx.com
Sat Jun 9 15:34:51 PDT 2012


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 doesn't _have_ static array literals. It only has dynamic array literals.

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

should definitely be optimized such that it doesn't do any heap allocations, 
but the array literal itself is dynamic regardless, as typeof indicates. The 
other place that this causes problems is templated functions. e.g.

void func(A)(A array)
    if(isStaticArray!A)
{}

func([1, 2, 3, 4, 5]);

will fail to compile. You're forced to create one on the stack first.

int[5] array = [1, 2, 3, 4, 5];
func(array);

That _might_ merit adding a syntax for indicating that an array literal is 
static.

However, in general, it's definitely true that the additional heap allocations 
that we currently see should just be optimized out by the compiler, and if 
that's all that we're trying to solve, then that's a matter of fixing the 
optimizer, not the language.

- Jonathan M Davis


More information about the Digitalmars-d mailing list