digitalmars.D.learn - How to initialize a associative array?
- Yuxuan Shui (5/5) Dec 23 2016 I tried this:
- Yuxuan Shui (3/9) Dec 23 2016 This example here: https://dlang.org/spec/hash-map.html, doesn't
- Nicholas Wilson (3/16) Dec 23 2016 Is this at global scope?
- Stefan Koch (3/9) Dec 24 2016 You cannot initialize an AA at compile-time.
- Carl Vogel (7/9) Dec 24 2016 This bit me when I was first starting out as well. I feel like
- Stefan Koch (5/7) Dec 25 2016 I do intend to fix this.
- Era Scarecrow (11/16) Dec 24 2016 For fun I'm throwing together a quick AA struct that you can
- Stefan Koch (5/25) Dec 25 2016 There are library solutions for AA like containers that can be
- Jack Applegame (10/16) Dec 25 2016 This works:
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
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
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:Is this at global scope? You need to use a `shared static this() { ... }` to initialise it.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
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
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
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
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: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.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
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: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 ?On Saturday, 24 December 2016 at 00:55:01 UTC, Yuxuan Shui wrote: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.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 25 2016
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