digitalmars.D.learn - Static initialization of associative arrays
- Chris Piker (18/18) Mar 11 2021 Hi D
- =?UTF-8?Q?Ali_=c3=87ehreli?= (38/40) Mar 11 2021 I use a shared static this() block:
- Chris Piker (9/19) Mar 11 2021 Hi Ali
- Imperatorn (8/51) Mar 11 2021 You can however do like this (cheating):
- H. S. Teoh (13/19) Mar 11 2021 [...]
- Chris Piker (11/16) Mar 11 2021 Hi H.S.T
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,
Mar 11 2021
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
Mar 11 2021
On Thursday, 11 March 2021 at 18:41:08 UTC, Ali Çehreli wrote:On 3/11/21 10:06 AM, Chris Piker wrote: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,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
Mar 11 2021
On Thursday, 11 March 2021 at 18:41:08 UTC, Ali Çehreli wrote:On 3/11/21 10:06 AM, Chris Piker wrote: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.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
Mar 11 2021
On Thu, Mar 11, 2021 at 06:06:35PM +0000, 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".
Mar 11 2021
On Thursday, 11 March 2021 at 19:12:34 UTC, H. S. Teoh wrote:On Thu, Mar 11, 2021 at 06:06:35PM +0000, 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,
Mar 11 2021