Thread overview
CTFE and assoc array
Jan 18, 2020
Andrey
Jan 18, 2020
Boris Carvajal
Jan 19, 2020
Andrey
Jan 19, 2020
user1234
Jan 21, 2020
H. S. Teoh
January 18, 2020
Hello,

Why this doesn't work?

> import std;
> 
> struct Qwezzz
> {
> 	shared static this()
>     {
>         qaz = qazMap;
>     }
> 
>     enum qazMap = ["rrr": "vv", "hty": "4ft6"];
>     static immutable string[string] qaz;
> }
> 
> void main()
> {
>     enum sorted = Qwezzz.qaz.keys.sort();
> }

The variable "qaz" is static immutable and doesn't work in CTFE.
January 18, 2020
On Saturday, 18 January 2020 at 20:54:20 UTC, Andrey wrote:
> Hello,
>
> Why this doesn't work?
>
>> import std;
>> 
>> struct Qwezzz
>> {
>> 	shared static this()
>>     {
>>         qaz = qazMap;
>>     }
>> 
>>     enum qazMap = ["rrr": "vv", "hty": "4ft6"];
>>     static immutable string[string] qaz;
>> }
>> 
>> void main()
>> {
>>     enum sorted = Qwezzz.qaz.keys.sort();
>> }
>
> The variable "qaz" is static immutable and doesn't work in CTFE.

There 3 issues here:

1. "shared static this()" is a runtime construct.

2. You can't initialize a static AA right now
https://dlang.org/spec/hash-map.html#static_initialization
Only "aa = null;" works.

3. CT and RT AA internals are different.

But you can get a workaround, more info
https://forum.dlang.org/post/egrcolfiqpuplahpoiov@forum.dlang.org
January 19, 2020
> On Saturday, 18 January 2020 at 21:44:35 UTC, Boris Carvajal wrote:

I read that thread. But:
> Deprecation: initialization of immutable variable from static this is deprecated.
> Use shared static this instead.

And we get? No CTFE with static immutable AA?
January 19, 2020
On Sunday, 19 January 2020 at 13:02:18 UTC, Andrey wrote:
>> On Saturday, 18 January 2020 at 21:44:35 UTC, Boris Carvajal wrote:
>
> I read that thread. But:
>> Deprecation: initialization of immutable variable from static this is deprecated.
>> Use shared static this instead.
>
> And we get? No CTFE with static immutable AA?

The problem is that the code for AA consists of runtime hooks.
So in practice even if your keys and values are available the compiler doesn't know how to build it and use it.

At some point what could be done is a kind of serialization at compile time and facilities for quick deser at runtime from the data segment but that doesn't change the fact that they could still not be used for CTFE or template metaprog.
January 21, 2020
On 1/19/20 8:02 AM, Andrey wrote:
>> On Saturday, 18 January 2020 at 21:44:35 UTC, Boris Carvajal wrote:
> 
> I read that thread. But:
>> Deprecation: initialization of immutable variable from static this is deprecated.
>> Use shared static this instead.

That should have been noted in the original thread. shared static this is required to initialize static immutables. Simple reason -- a static immutable is shared between all threads, so you shouldn't be initializing shared static data in every thread that gets created.

> And we get? No CTFE with static immutable AA?

Right, CFTE cannot access static immutable AA that are initialized at runtime.

But you can access the enum.

e.g. (yes it's horrid):

enum sorted = Qwezzz.qazMap.keys.sort();

It would be really cool to make AA's at CTFE change into runtime AAs when used at runtime, and be accessible as compile-time AAs otherwise.

-Steve
January 21, 2020
On Tue, Jan 21, 2020 at 04:51:35PM -0500, Steven Schveighoffer via Digitalmars-d-learn wrote: [...]
> It would be really cool to make AA's at CTFE change into runtime AAs when used at runtime, and be accessible as compile-time AAs otherwise.
[...]

I've been wishing for this since the early days when I first joined D.


T

-- 
Маленькие детки - маленькие бедки.