Compile time initialization of AA

Seb seb at wilzba.ch
Fri Mar 23 23:15:51 UTC 2018


On Friday, 23 March 2018 at 22:43:47 UTC, Xavier Bigand wrote:
> I am trying to initialize an global immutable associative array 
> of structs, but it doesn't compile.
> 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.
>
> Here is my code :
> struct EntryPoint
> {
>     string  moduleName;
>     string  functionName;
>     bool    beforeForwarding = false;
> }
>
> immutable EntryPoint[string]  entryPoints = [
>     "wglDescribePixelFormat": 
> {moduleName:"opengl32.forward_initialization", 
> functionName:"wglDescribePixelFormat"}
> ];

Try this:

https://run.dlang.io/is/b7VQVB

tl;dr: you can instantiate immutable data in the module 
constructors.

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

static immutable EntryPoint[string] entryPoints;

shared static this()
{
     EntryPoint p = {moduleName:"opengl32.forward_initialization", 
functionName:"wglDescribePixelFormat"};
     entryPoints = ["wglDescribePixelFormat" : p];
}

void main()
{
     import std.stdio;
     entryPoints.writeln;
}
---


More information about the Digitalmars-d-learn mailing list