Re: Static initialization of associative arrays
On Thursday, 11 March 2021 at 18:41:08 UTC, Ali Çehreli wrote: On 3/11/21 10:06 AM, Chris Piker wrote: >https://dlang.org/spec/hash-map.html#static_initialization > > that this feature is not yet implemented. I use a shared static this() block: immutable string[int] aa; shared static this() { aa = [ 1: "one" ]; } void main() { assert(aa.length == 1); } And it is possible to build an AA at compile time as the initial value but it still needs a trivial assigment to the immutable variable. Assuming that we have the following file at compile time named 'my_aa': --- 8< --- 1 one 2 two --- 8< --- And remembering that we have to use the -J switch when compiling (e.g. as -J.), you can parse and build an AA from that file like this. (Sorry for insisting on the the range style; it can be done in other ways). immutable string[int] aa; shared static this() { import std.algorithm; import std.range; import std.typecons; import std.conv; enum compileTimeAA = import("my_aa") .splitter .chunks(2) .map!(a => tuple(a.front.to!int, a.dropOne.front)) .assocArray; aa = compileTimeAA; } import std.stdio; void main() { writeln(aa); } Ali You can however do like this (cheating): https://run.dlang.io/is/9TSfAB "The variable myOptions is assigned the result of the literal at runtime. But because it's immutable, the compiler knows what's in it. So it can extrapolate back to the literal what it is at compile time" We had a discussion in Discord about this last week.
Re: Static initialization of associative arrays
On Thursday, 11 March 2021 at 19:12:34 UTC, H. S. Teoh wrote: On Thu, Mar 11, 2021 at 06:06:35PM +, Chris Piker via immutable int[string] aa; shared static this() { aa = [ "abc": 123, "def": 456, /* ... */ ]; } Hi H.S.T Yes, I'm using static if, but do you know if direct implementation of immutable associative array assignment (as given on the language spec page) will be implemented at some point? This is not a make-or-break feature that I need, just an opportunity to gain meta-information. The actual problem is easy to work around, I'm mostly asking out of curiosity to learn more about the D ecosystem and how it functions. Thanks,
Re: Static initialization of associative arrays
On Thursday, 11 March 2021 at 18:41:08 UTC, Ali Çehreli wrote: On 3/11/21 10:06 AM, Chris Piker wrote: >https://dlang.org/spec/hash-map.html#static_initialization > > that this feature is not yet implemented. I use a shared static this() block: immutable string[int] aa; shared static this() { aa = [ 1: "one" ]; } Ali Hi Ali Always good to hear from an author. I picked up a copy of your book the other day... nice work! I appreciate the tips on compile-time execution (hadn't thought of that), but do you know how I find out about the implementation status of a feature in D? I'm aware of the DIP system, but I though DIPs were just for language changes. Thanks,
Re: Static initialization of associative arrays
On Thu, Mar 11, 2021 at 06:06:35PM +, Chris Piker via Digitalmars-d-learn wrote: [...] > Today I ran across a situation where an immutable associative array > would be handy. While perusing the language spec I noticed here: > > https://dlang.org/spec/hash-map.html#static_initialization > > that this feature is not yet implemented. [...] The subsequent section on the linked page gives the solution / workaround. Just declare your immutable AA without initialization, and initialize it in a static ctor: immutable int[string] aa; shared static this() { aa = [ "abc": 123, "def": 456, /* ... */ ]; } T -- INTEL = Only half of "intelligence".
Re: Static initialization of associative arrays
On 3/11/21 10:06 AM, Chris Piker wrote: >https://dlang.org/spec/hash-map.html#static_initialization > > that this feature is not yet implemented. I use a shared static this() block: immutable string[int] aa; shared static this() { aa = [ 1: "one" ]; } void main() { assert(aa.length == 1); } And it is possible to build an AA at compile time as the initial value but it still needs a trivial assigment to the immutable variable. Assuming that we have the following file at compile time named 'my_aa': --- 8< --- 1 one 2 two --- 8< --- And remembering that we have to use the -J switch when compiling (e.g. as -J.), you can parse and build an AA from that file like this. (Sorry for insisting on the the range style; it can be done in other ways). immutable string[int] aa; shared static this() { import std.algorithm; import std.range; import std.typecons; import std.conv; enum compileTimeAA = import("my_aa") .splitter .chunks(2) .map!(a => tuple(a.front.to!int, a.dropOne.front)) .assocArray; aa = compileTimeAA; } import std.stdio; void main() { writeln(aa); } Ali
Static initialization of associative arrays
Hi D At work I've begun writing programs in D that I would typically write in python. My goal is to get away from split python/C development and just use one language most of the time. Today I ran across a situation where an immutable associative array would be handy. While perusing the language spec I noticed here: https://dlang.org/spec/hash-map.html#static_initialization that this feature is not yet implemented. So where do I go learn about the status of a feature? I'd like to check on the progress of this particular one. For now I'll use a 'static if' construct to ready the array. As someone with a python background it's nice if I can avoid extra lines of code for straight forward ideas, but I understand that switching to faster compiled code doesn't come for free and some extra boiler-plate will be needed. By the way I do like that intended features are documented up front, even if no one's had time to work on them. Thanks,
static initialization of associative arrays
Is it yet possible to statically initialize an associative array? If so, please point me to the documentation. I am using DMD v2.028. Thanks, Andrew
static initialization of associative arrays
Is it yet possible to statically initialize an associative array? If so, please point me to the documentation. I am using DMD v2.028. Currently I'm able to do this: import std.stdio; string[string] types; static this(){ types = [ void:void, bool:bool ]; } void main(){ writeln(types); } Output = [void:void,bool:bool] which is exactly what I want. However, removing static this() results in an error. string[string] types = [ void:void, bool:bool ]; Result: api.d(77): Error: non-constant expression [void:void,bool:bool] How do I make the initialization constant? Thanks, Andrew
Re: static initialization of associative arrays
may be you can raplace string with char[] ?
Re: static initialization of associative arrays
Tyro[a.c.edwards] wrote: Is it yet possible to statically initialize an associative array? If so, please point me to the documentation. I am using DMD v2.028. Currently I'm able to do this: import std.stdio; string[string] types; static this(){ types = [ void:void, bool:bool ]; } void main(){ writeln(types); } Output = [void:void,bool:bool] which is exactly what I want. However, removing static this() results in an error. string[string] types = [ void:void, bool:bool ]; Result: api.d(77): Error: non-constant expression [void:void,bool:bool] How do I make the initialization constant? Thanks, Andrew I think Walter said something a while back to the effect that making it possible to statically initialise AAs isn't feasible because it requires setting up a complex structure on the heap. The best you could do would be to *pretend* to statically initialise them, and actually really initialise them in a module ctor. Which is exactly what you currently have to do. Could be wrong; that's just what I remember from the last time this came up. -- Daniel
Re: static initialization of associative arrays
Daniel Keep: I think Walter said something a while back to the effect that making it possible to statically initialise AAs isn't feasible because it requires setting up a complex structure on the heap. At the moment you can't statically initialize a built-in AA in D. But with a small change in D AAs such memory may be allocated statically too, when the program starts. What's the advantage of doing this? Having quicker startup times? Bye, bearophile