www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D1: How to declare an Associative array with data

reply jicman <jicman gmail.com> writes:
Yes, I know.  I am using D1, and I love it. ;-)

Greetings.

I know that I can declare an array with data, ie.

string[] d = ["1","2","three","4];

and this will be done at compiled time.  I want to do the same 
with an associative array., ie.

int[string] MyDigits = ["0" = 0, "1" = 1, "2" = 2];

That does not work.  I also tried,

int[string] MyDigits = ["0" : 0, "1" : 1, "2" : 2];

Nope, it does not work either.  Any thoughts?  thanks.

josé
Sep 04 2019
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 4 September 2019 at 13:33:02 UTC, jicman wrote:
 string[] d = ["1","2","three","4];

 and this will be done at compiled time.  I want to do the same 
 with an associative array., ie.
That's actually only at compile time if it is in static scope, if it inside a function, it is a runtime array. But anyway..
 int[string] MyDigits = ["0" : 0, "1" : 1, "2" : 2];

 Nope, it does not work either.  Any thoughts?  thanks.
this works at runtime inside a function, but does not work at compile time at all, in any version of D (as of right now); the compile-time AA to static data was never implemented. You could possibly do a custom implementation though.
Sep 04 2019
parent jicman <jicman gmail.com> writes:
On Wednesday, 4 September 2019 at 13:39:05 UTC, Adam D. Ruppe 
wrote:
 On Wednesday, 4 September 2019 at 13:33:02 UTC, jicman wrote:
 string[] d = ["1","2","three","4];

 and this will be done at compiled time.  I want to do the same 
 with an associative array., ie.
That's actually only at compile time if it is in static scope, if it inside a function, it is a runtime array.
True.
 int[string] MyDigits = ["0" : 0, "1" : 1, "2" : 2];

 Nope, it does not work either.  Any thoughts?  thanks.
this works at runtime inside a function, but does not work at compile time at all, in any version of D (as of right now); the compile-time AA to static data was never implemented.
Darn it! I tried to see if I could get it set at compiled time. Thanks, and thanks for your wonderful knowledge and gift to D. Big fan of your work. Thanks. josé
Sep 04 2019
prev sibling parent Kagamin <spam here.lot> writes:
Maybe something like this 
https://forum.dlang.org/post/hloitwqnisvtgfougncf forum.dlang.org
Sep 05 2019