Thread overview
Create an associative array with function pointers as the value
Apr 20, 2022
rempas
Apr 20, 2022
vit
Apr 20, 2022
rempas
Apr 20, 2022
rikki cattermole
Apr 20, 2022
rempas
April 20, 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:

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:

void function(ref file_struct)

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

// 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?

April 20, 2022

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:

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

shared static this(){
	common_identifiers = [
      "let"     : &parse_let,
      // "macro"   : &parse_macro,
    ];
}
April 20, 2022

On Wednesday, 20 April 2022 at 11:10:18 UTC, vit wrote:

>

You need shared static this for initializing immutable AA:

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!

April 21, 2022
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.
April 20, 2022
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...