www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Static Initialization of AAs - when?

reply arandomonlooker <shdkahdkhhhhhii gmail.com> writes:
Hello.
I am posting here for the first time as a beginner to the D 
programming language.
I was curious about the fact that static initialization of 
associative arrays, while being present in the specification, it 
has not yet been implemented in the language.
There is a plan to implement it in the future?
Thank you all in advance.
May 28
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
On 5/28/22 8:36 PM, arandomonlooker wrote:
 Hello.
 I am posting here for the first time as a beginner to the D programming 
 language.
 I was curious about the fact that static initialization of associative 
 arrays, while being present in the specification, it has not yet been 
 implemented in the language.
 There is a plan to implement it in the future?
 Thank you all in advance.
You can do this with my [newaa project](https://code.dlang.org/packages/newaa): ```d import schlib.newaa; auto myAA = Hash!(string, int)(["hello" : 1, "world" : 2]); void main() { int[string] aa = myAA.asAA; writeln(aa); } ``` Not much niceties there yet, notably, it doesn't print out well, or have a good factory function. You also cannot reinterpret as a builtin AA at compile time, because the reinterpret cast isn't possible. But you can just use it as a drop-in replacement for builtin AAs. It actually is binary compatible, so you can have both a normal builtin AA reference to it, and a `Hash` reference, and both will work. -Steve
May 29
next sibling parent arandomonlooker <shdkahdkhhhhhii gmail.com> writes:
On Sunday, 29 May 2022 at 15:34:09 UTC, Steven Schveighoffer 
wrote:
 On 5/28/22 8:36 PM, arandomonlooker wrote:
 [...]
You can do this with my [newaa project](https://code.dlang.org/packages/newaa): ```d import schlib.newaa; auto myAA = Hash!(string, int)(["hello" : 1, "world" : 2]); void main() { int[string] aa = myAA.asAA; writeln(aa); } ``` Not much niceties there yet, notably, it doesn't print out well, or have a good factory function. You also cannot reinterpret as a builtin AA at compile time, because the reinterpret cast isn't possible. But you can just use it as a drop-in replacement for builtin AAs. It actually is binary compatible, so you can have both a normal builtin AA reference to it, and a `Hash` reference, and both will work. -Steve
Thank you. This is going to be useful to me anyways.
May 29
prev sibling parent reply Sergey <kornburn yandex.ru> writes:
On Sunday, 29 May 2022 at 15:34:09 UTC, Steven Schveighoffer 
wrote:
 On 5/28/22 8:36 PM, arandomonlooker wrote:
 But you can just use it as a drop-in replacement for builtin 
 AAs. It actually is binary compatible, so you can have both a 
 normal builtin AA reference to it, and a `Hash` reference, and 
 both will work.

 -Steve
Hi Steven! What about speed and performance? Did you compare it with built-in AA?
May 30
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 5/30/22 4:39 PM, Sergey wrote:
 On Sunday, 29 May 2022 at 15:34:09 UTC, Steven Schveighoffer wrote:
 On 5/28/22 8:36 PM, arandomonlooker wrote:
 But you can just use it as a drop-in replacement for builtin AAs. It 
 actually is binary compatible, so you can have both a normal builtin 
 AA reference to it, and a `Hash` reference, and both will work.
Hi Steven! What about speed and performance? Did you compare it with built-in AA?
No, but if anything, I'd expect it to be a tad bit faster, since it doesn't use TypeInfo for many things such as hash calculation, and it can be inlined. It might be slower in CTFE, since the interpreter is running the actual code instead of the builtin CTFE AA. However, you can use the builtin CTFE AA, and then just return a `Hash` as a result. Note that all the factors and the algorithms are identical to how druntime does it (I copied that code verbatim, which is why the copyright is from there). It has to be, because its aim is to be binary compatible. -Steve
May 30