How to initialize a global array of structures in D?

To help my one-year search for filling SO with D (= p) questions, I ran into another problem; initializing an array of structures globally. Note:

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 documents in Digital Mars , I do not see anything completely obvious to me, so I will again turn to the brave inhabitants! I assume that the error message does not have much to do with the real problem, since, of course, [0] is an identifier (but dmdconsiders it a declarator that AFAICT looks through docs , is that not ??

+3
source share
2 answers

, . ?

A[2] as = [
    {0, 0.0f},
    {5, 5.2f}
];

, , as[0] :

as[0] = {0, 0.0f};
as[0] = {1, 1.0f};

as[0] ? , .

, D :

A[2] as = [
    0: {0, 0.0f},
    1: {5, 5.2f}
];

, (, A[10]) . . Arrays D.

+6

,

struct A
{
    int a;
    float b;
}

A[2] as;
as[0] = A(0, 0.0f);
as[1] = A(5, 5.2f);

void main() {}

, , (.. static opCall). . StructLiteral

- , , , . , , , .

+3

Source: https://habr.com/ru/post/1715940/


All Articles