www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Create an associative array with function pointers as the value

reply rempas <rempas tutanota.com> writes:
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?
Apr 20 2022
parent reply vit <vit vit.vit> writes:
On Wednesday, 20 April 2022 at 10:42:59 UTC, rempas wrote:
 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:

 [...]
You need shared static this for initializing immutable AA: ```d immutable void function(ref file_struct)[string] common_identifiers; shared static this(){ common_identifiers = [ "let" : &parse_let, // "macro" : &parse_macro, ]; } ```
Apr 20 2022
parent reply rempas <rempas tutanota.com> writes:
On Wednesday, 20 April 2022 at 11:10:18 UTC, vit wrote:
 You need shared static this for initializing immutable AA:

 ```d
 immutable void function(ref file_struct)[string] 
 common_identifiers;

 shared static this(){
 	common_identifiers = [
       "let"     : &parse_let,
       // "macro"   : &parse_macro,
     ];
 }
 ```
Unfortunately, this will not work for me as it uses "TypeInfo" and it is not available with "-betterC". Thank you for trying to help regardless!
Apr 20 2022
parent reply rikki cattermole <rikki cattermole.co.nz> writes:
On 21/04/2022 2:15 AM, rempas wrote:
 Unfortunately, this will not work for me as it uses "TypeInfo" and it is 
 not available with "-betterC". Thank you for trying to help regardless!
You can't use AA's in -betterC. The implementation is not templated and is in druntime.
Apr 20 2022
parent rempas <rempas tutanota.com> writes:
On Wednesday, 20 April 2022 at 14:29:33 UTC, rikki cattermole 
wrote:
 On 21/04/2022 2:15 AM, rempas wrote:
 Unfortunately, this will not work for me as it uses "TypeInfo" 
 and it is not available with "-betterC". Thank you for trying 
 to help regardless!
You can't use AA's in -betterC. The implementation is not templated and is in druntime.
Oh! Thank you! I'm an idiot for not knowing that...
Apr 20 2022