www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - CTFE and assoc array

reply Andrey <saasecondbox yandex.ru> writes:
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.
Jan 18 2020
parent reply Boris Carvajal <boris2.9 gmail.com> writes:
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
Jan 18 2020
parent reply Andrey <saasecondbox yandex.ru> writes:
 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?
Jan 19 2020
next sibling parent user1234 <user12324 1234.de> writes:
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.
Jan 19 2020
prev sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jan 21 2020
parent "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
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 -- Маленькие детки - маленькие бедки.
Jan 21 2020