Static associative array expressions

Stefan Koch uplink.coder at googlemail.com
Sun Sep 19 05:43:04 UTC 2021


So ... I have had some time on my hands and since have recently 
been working on the newCTFE VM and re-implemented the ABI proper 
...

I thought actually ... this is an ABI issue.
If the compiler knew about the layout of the associative array we 
could emit that layout into the binary just as we would at 
runtime.
provided of course that the hash-function is CTFE-able.

```d
import core.internal.hash;
static immutable ctAA = ["abc":"v",  "ab":"vv", "c" : "vvv", "d" 
: "vvvv"];

struct CTFEHashableString
{
     string s;
     alias s this;
     hash_t toHash() @safe pure nothrow @nogc
     {
         return hashOf(s);
     }
}

void main()
{
     auto rtAA = ["abc":"v",  "ab":"vv", "c" : "vvv", "d" : 
"vvvv"];

     import core.stdc.stdio;
     printf("ctAA.length: %zu\n", ctAA.length);
     printf("rtAA.length: %zu\n", rtAA.length);

     printf(" ----- compile time AA ------\n");
     foreach(k,v; ctAA)
     {
         printf("%s:%s\n", k.ptr, v.ptr);
     }

     printf(" ------- runttime AA --------\n");
     foreach(k,v; rtAA)
     {
         printf("%s:%s\n", k.ptr, v.ptr);
     }
}

```

This code now works on my computer ;)
Yes this means the compiler has to be changed if we change the 
runtime structure of the AA.
And there is a tiny bit of code duplication, however I do think 
that the parity of compile-time and run-time features is worth it 
...

What do you guys think?




More information about the Digitalmars-d mailing list