digitalmars.D.learn - CTFE and assoc array
- Andrey (3/20) Jan 18 2020 The variable "qaz" is static immutable and doesn't work in CTFE.
- Boris Carvajal (9/29) Jan 18 2020 There 3 issues here:
- Andrey (2/7) Jan 19 2020 And we get? No CTFE with static immutable AA?
- user1234 (8/15) Jan 19 2020 The problem is that the code for AA consists of runtime hooks.
- Steven Schveighoffer (13/20) Jan 21 2020 That should have been noted in the original thread. shared static this
- H. S. Teoh (7/9) Jan 21 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.
Jan 18 2020
On Saturday, 18 January 2020 at 20:54:20 UTC, Andrey wrote:Hello, Why this doesn't work?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.orgimport 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
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
On Sunday, 19 January 2020 at 13:02:18 UTC, Andrey wrote: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.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
On 1/19/20 8:02 AM, Andrey wrote: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.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?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
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