Do you want to help me fix static AA initialization?
RazvanN
razvan.nitu1305 at gmail.com
Tue Jul 25 08:45:20 UTC 2023
On Thursday, 29 June 2023 at 19:48:40 UTC, Steven Schveighoffer
wrote:
> 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
Maybe this has been addressed in previous discussions, but why
not rewrite:
```d
int[string] b = ["mimi" : 2];
```
to
```d
int[string] b;
static this()
{
b = ["mimi" : 2];
}
```
I know that we have problems with cycle detection in module
constructors, but assuming this will be fixed by Adam's proposal,
this seems like a lot easier to implement and does not stress
compilation time.
RazvanN
More information about the Digitalmars-d
mailing list