Static Associative Array

bearophile bearophileHUGS at lycos.com
Sun Mar 6 18:32:54 PST 2011


Peter Lundgren:

> If not, then what is the D way to initialize a static field of a struct or class a
> la Java's static initializer blocks? I don't mind constructing the associative
> array at run-time if I have to, but I can't afford to do it more than the once needed.

Is this good enough?

struct Foo {
    static int[int] aa;
    static this() {
        aa[0] = 1;
    }
}

void main() {
    assert(Foo.aa[0] == 1);
}

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list