Do you want to help me fix static AA initialization?

Steven Schveighoffer schveiguy at gmail.com
Thu Jun 29 19:48:40 UTC 2023


Hi everyone,

I have a fix for static associative array initialization. Basically, I 
have a project called `newaa` which is a library-based copy of D's 
associative arrays. The url is here: https://github.com/schveiguy/newaa

This allows static initialization of AAs, e.g.:

```d

Hash!(string, string) ctaa = [
    "name" : "Steve",
    "username" : "schveiguy"
];

void main()
{
    string[string] aa = ctaa.asAA;
}

```

If you look at the project, you can see that `asAA` actually is just a 
reinterpret cast of the internals, since the layout is exactly the same. 
The actual AA is built at compile time, and just used at runtime, 
without any issues (and yes, the mutability is fine).

I want to make this part of the language/library, such that you can just 
straight-up initialize AA's at compile time and use them at runtime.

However, I need the compiler's help. Why? Because CTFE does not let you 
retinterpret cast, plus it doesn't have a distinct type for "runtime AA" 
vs. "compile time AA" (these are distinct types). So I need some kind of 
hook to cross the barrier, where the compiler ignores the return type 
not being an AA and just bludgeons it into an AA location. I also need 
it to call this hook *only* when going to convert the AA from CT to RT, 
including if it is a member in some CT structure.

Does anyone have the skills to help me? Hit me here, or on discord, or 
send me an email: schveiguy at gmail.com

BTW, this fix in principle is tentatively approved by Walter for 
inclusion (assuming everything we thought of is kosher with the 
language). As long as it doesn't break any existing code, and just 
allows currently non-working code to just work.

-Steve


More information about the Digitalmars-d mailing list