How to make a global immutable associative array?

bearophile bearophileHUGS at lycos.com
Tue Mar 18 17:33:23 PDT 2014


dnspies:

> I want to create a global immutable associative array and have 
> it be accessible from anywhere at compile-time.  How can I do 
> that?
>
> With:
> immutable int[int] aa = [1:2,3:4];
>
> I get:
> source/thing.d(1): Error: non-constant expression [1:2, 3:4]
>
> And with:
> immutable int[int] aa;
>
> static this(){
> 	aa = [1:2,3:4];
> }
>
> int c = aa[3];
>
> I get:
> source/thing.d(7): Error: static variable aa cannot be read at 
> compile time


immutable int[int] aa;
pure static this() {
     aa = [1: 2, 3: 4];
}
void main() {
     int c = aa[3];
}


Bye,
bearophile


More information about the Digitalmars-d mailing list