Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
December 24, 2016 How to initialize a associative array? | ||||
---|---|---|---|---|
| ||||
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? |
December 24, 2016 Re: How to initialize a associative array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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. |
December 24, 2016 Re: How to initialize a associative array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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.
|
December 24, 2016 Re: How to initialize a associative array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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.
|
December 25, 2016 Re: How to initialize a associative array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | 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?
|
December 25, 2016 Re: How to initialize a associative array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | 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.
|
December 25, 2016 Re: How to initialize a associative array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Yuxuan Shui | 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];
}
|
December 25, 2016 Re: How to initialize a associative array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Era Scarecrow | 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 ?
|
December 25, 2016 Re: How to initialize a associative array? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carl Vogel | 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.
|
Copyright © 1999-2021 by the D Language Foundation