How to initialise static arrays in D without a GC allocation?

Viewed 582

In D, all array literals are dynamic arrays, and are therefore allocated by the GC.

Even in this simple example:

int[3] a = [10, 20, 30];

The array is heap-allocated and then copied into a.

How are you supposed to initialise a static array without heap-allocation?

You could do it manually:

int[3] a = void;
a[0] = 10;
a[1] = 20;
a[2] = 30;

But this is tedious at best.

Is there a better way?

5 Answers
Related