In aid of my one-man quest to populate SO with D questions (=p), I've run into another problem; initialising an array of structs globally. Observe:
struct A
{
int a;
float b;
}
A[2] as;
as[0] = {0, 0.0f};
as[1] = {5, 5.2f};
void main() {}
Results in:
$ dmd wtf.d
wtf.d(8): no identifier for declarator as[0]
wtf.d(9): no identifier for declarator as[1]
Looking through the docs at Digital Mars, I can't really see anything entirely obvious to me, so I turn once more to the brave denizens of Stack Overflow! I'm guessing the error message doesn't have much to do with the real problem, as surely as[0] is an identifier (but dmd
thinks it's a declarator, which AFAICT looking over the docs, it isn't)?
I don't think you can initialise elements on a per-element basis like that. Would this work?
A[2] as = [
{0, 0.0f},
{5, 5.2f}
];
Consider what would happen if, in your example, you mentioned as[0]
more than once:
as[0] = {0, 0.0f};
as[0] = {1, 1.0f};
What would the value of as[0]
be at program initialisation? This is becoming more like statements rather than initialisers.
Note that in D, you can initialise array elements at specific indexes like this:
A[2] as = [
0: {0, 0.0f},
1: {5, 5.2f}
];
This would be useful if you have a larger array (such as A[10]
) and only need to initialise some of the elements. See Arrays in the D reference documentation for more information.