On 03/23/2018 03:43 PM, Xavier Bigand wrote:
> I am trying to initialize an global immutable associative array of
> structs, but it doesn't compile.

Current solution is to initialize the AA in a 'static this()' block or ('shared static this()').

> I am getting the following error message : "Error: not an associative
> array initializer".
>
> As I really need to store my data for a compile time purpose if we can't
> do that with AA, I'll use arrays instead.

You can create and use AAs at compile time, which may be an acceptable use for your case as well:

struct EntryPoint
{
    string  moduleName;
    string  functionName;
    bool    beforeForwarding = false;
}

EntryPoint[string] makeEntryPoints() {
    EntryPoint[string] entryPoints = [
"wglDescribePixelFormat": EntryPoint("opengl32.forward_initialization", "wglDescribePixelFormat")
    ];
    return entryPoints;
}

string useEntryPoints(EntryPoint[string] aa) {
    return aa["wglDescribePixelFormat"].moduleName;
}

void main() {
    enum s = useEntryPoints(makeEntryPoints());
    static const s2 = s;
}

The computations of 's' and 's2' in main are performed at compile time. If you can getaway with that, great. :) Otherwise, you have to initialize at runtime in a 'shared static this()' block, which can still be 'immutable':

// NOTE the addition of 'pure':
EntryPoint[string] makeEntryPoints() pure {
    // Same as above...
}

immutable EntryPoint[string] entryPoints;
shared static this() {
    // Yes, can initialize immutable:
    entryPoints = makeEntryPoints();
}

Ali

Reply via email to