Assigning global and static associative arrays
Mike Parker
aldacron at gmail.com
Fri Aug 31 09:36:15 PDT 2012
On 9/1/2012 1:31 AM, Mike Parker wrote:
> On 8/31/2012 11:38 PM, ixid wrote:
>> Why does this not work:
>>
>> int[string] dayNumbers =
>> [ "Monday" : 0, "Tuesday" : 1, "Wednesday" : 2,
>> "Thursday" : 3, "Friday" : 4, "Saturday" : 5,
>> "Sunday" : 6 ];
>>
>> void main() {
>> //Stuff
>> }
>>
>> With the error 'non-constant expression'? This error also seems to
>> prevent me making static associative arrays in functions elegantly.
>
> Non-const variables cannot be initialized at module-scope. If you don't
> need to modify the aa, declaring it immutable should allow the
> initialization to work. Otherwise, you can initialize it using a static
> module constructor.
>
> int[string] dayNumbers;
>
> static this()
> {
> dayNumbers = ...;
> }
>
> void main() {...}
Nevermind. I spoke too soon. It's the literal ["Monday" : 0, ...] that's
not constant. You'll have to use the module constructor.
More information about the Digitalmars-d-learn
mailing list