www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to initialize a associative array?

reply Yuxuan Shui <yshuiv7 gmail.com> writes:
I tried this:

     immutable int[char] xx = ['Q':0, 'B':1, 'N':2, 'R':3, 'P':4];

And got a "non-constant expression" error (with or without 
'immutable').

What's the correct way?
Dec 23 2016
next sibling parent reply Yuxuan Shui <yshuiv7 gmail.com> writes:
On Saturday, 24 December 2016 at 00:55:01 UTC, Yuxuan Shui wrote:
 I tried this:

     immutable int[char] xx = ['Q':0, 'B':1, 'N':2, 'R':3, 
 'P':4];

 And got a "non-constant expression" error (with or without 
 'immutable').

 What's the correct way?
This example here: https://dlang.org/spec/hash-map.html, doesn't work either.
Dec 23 2016
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Saturday, 24 December 2016 at 00:57:04 UTC, Yuxuan Shui wrote:
 On Saturday, 24 December 2016 at 00:55:01 UTC, Yuxuan Shui 
 wrote:
 I tried this:

     immutable int[char] xx = ['Q':0, 'B':1, 'N':2, 'R':3, 
 'P':4];

 And got a "non-constant expression" error (with or without 
 'immutable').

 What's the correct way?
This example here: https://dlang.org/spec/hash-map.html, doesn't work either.
Is this at global scope? You need to use a `shared static this() { ... }` to initialise it.
Dec 23 2016
prev sibling next sibling parent reply Stefan Koch <uplink.coder googlemail.com> writes:
On Saturday, 24 December 2016 at 00:55:01 UTC, Yuxuan Shui wrote:
 I tried this:

     immutable int[char] xx = ['Q':0, 'B':1, 'N':2, 'R':3, 
 'P':4];

 And got a "non-constant expression" error (with or without 
 'immutable').

 What's the correct way?
You cannot initialize an AA at compile-time. Because AA's are provided by druntime and the ABI is not stable.
Dec 24 2016
next sibling parent reply Carl Vogel <carljv gmail.com> writes:
On Saturday, 24 December 2016 at 11:21:11 UTC, Stefan Koch wrote:

 You cannot initialize an AA at compile-time.
 Because AA's are provided by druntime and the ABI is not stable.
This bit me when I was first starting out as well. I feel like there's really very little documentation on this, and I think most people's expect that you'd be able to initialize AAs from literals at compile time, just like normal arrays. Does anyone know if compile-time initialization of AAs is something that will be fixed?
Dec 24 2016
parent Stefan Koch <uplink.coder googlemail.com> writes:
On Sunday, 25 December 2016 at 03:12:09 UTC, Carl Vogel wrote:
 Does anyone know if compile-time initialization of AAs is
 something that will be fixed?
I do intend to fix this. The method is rather simple, Given the aa implantation in d-runtime is made ctfe-able everything that causes trouble currently will just work.
Dec 25 2016
prev sibling parent reply Era Scarecrow <rtcvb32 yahoo.com> writes:
On Saturday, 24 December 2016 at 11:21:11 UTC, Stefan Koch wrote:
 On Saturday, 24 December 2016 at 00:55:01 UTC, Yuxuan Shui 
 wrote:
 What's the correct way?
You cannot initialize an AA at compile-time. Because AA's are provided by druntime and the ABI is not stable.
For fun I'm throwing together a quick AA struct that you can make at compile time. It's experimental and may be slow for compiling as it needs to find a size of numbers where the hashes don't overlap. Of course you can't add to it, you can't delete elements (key limitations) but you could in theory change the value; So this would be better for immutable objects or having a fixed number of pre-known keys. If anyone is interested I can share my sources fairly soon once I'm satisfied the required features are done. So far opIndex and generation works fine, but range/foreach over it is incomplete.
Dec 24 2016
parent Stefan Koch <uplink.coder googlemail.com> writes:
On Sunday, 25 December 2016 at 05:45:14 UTC, Era Scarecrow wrote:
 On Saturday, 24 December 2016 at 11:21:11 UTC, Stefan Koch 
 wrote:
 On Saturday, 24 December 2016 at 00:55:01 UTC, Yuxuan Shui 
 wrote:
 What's the correct way?
You cannot initialize an AA at compile-time. Because AA's are provided by druntime and the ABI is not stable.
For fun I'm throwing together a quick AA struct that you can make at compile time. It's experimental and may be slow for compiling as it needs to find a size of numbers where the hashes don't overlap. Of course you can't add to it, you can't delete elements (key limitations) but you could in theory change the value; So this would be better for immutable objects or having a fixed number of pre-known keys. If anyone is interested I can share my sources fairly soon once I'm satisfied the required features are done. So far opIndex and generation works fine, but range/foreach over it is incomplete.
There are library solutions for AA like containers that can be used at compiletime. Maybe you could help polishing them ? So we can eventually get them into druntime ?
Dec 25 2016
prev sibling parent Jack Applegame <japplegame gmail.com> writes:
On Saturday, 24 December 2016 at 00:55:01 UTC, Yuxuan Shui wrote:
 I tried this:

     immutable int[char] xx = ['Q':0, 'B':1, 'N':2, 'R':3, 
 'P':4];

 And got a "non-constant expression" error (with or without 
 'immutable').

 What's the correct way?
This works: void main() { immutable int[char] xx = ['Q':0, 'B':1, 'N':2, 'R':3, 'P':4]; } If you want to initialize global variable, then immutable int[char] xx; shared static this() { xx = ['Q':0, 'B':1, 'N':2, 'R':3, 'P':4]; }
Dec 25 2016