Create an associative array with function pointers as the value

rempas rempas at tutanota.com
Wed Apr 20 10:42:59 UTC 2022


I'm trying to create an associative array where the keys will be 
a "string" type and the values will be function pointers. I'm 
using a custom type is called "file_struct" and for anyone that 
wants to try specifically with this type, the definition is the 
following:

```d
struct file_struct {
   FILE* file;
   const char* name;
   size_t ln, cn, size;
   str identifier, type_buffer, type_buffer_tmp;

   void inc_ln() {
     this.cn = 0;
     this.ln++;
   }
}
```

The function pointers will point to a function that doesn't 
return anything and takes only one parameter that is of my custom 
"file_struct" type. More specifically, the signature is the 
following:

```d
void function(ref file_struct)
```

So with that been said, I tried to create an associative array 
with the following code:

```d
// Suppose that the function exists in another file
void parse_let(ref file_struct file) {}

immutable void function(ref file_struct)[string] 
common_identifiers = [
   "let"     : &parse_let,
   // "macro"   : &parse_macro,
];
```

When I try to compile it (I can only compile with "ldc2" because 
my code contains gcc-style inline assembly), I get the following 
error message:

```
main.d(12,71): Error: expression `["let":& parse_let]` is not a 
constant
```

Of course the file name and the number of line is relative to me 
case. Any ideas?


More information about the Digitalmars-d-learn mailing list