Associative array literal is non-constant?

pplantinga plantinga.peter at gmail.com
Fri Jan 17 03:18:46 PST 2014


On Saturday, 4 February 2012 at 06:45:08 UTC, H. S. Teoh wrote:
> On Fri, Feb 03, 2012 at 10:18:18PM -0800, H. S. Teoh wrote:
>> Why does the following code give a compiler error?
>> 
>> 	static int[string] table = ["abc":1, "def":2, "ghi":3];
>> 
>> Error message is:
>> 
>> 	prog.d:3: Error: non-constant expression 
>> ["abc":1,"def":2,"ghi":3]
>> 
>> How is a literal non-constant?
> [...]
>
> Ugh. Just found this:
>
> 	http://d.puremagic.com/issues/show_bug.cgi?id=6238
>
> Further testing shows that assoc array literals can't be used 
> outside of
> function scope at all, for example:
>
> 	// (in package scope)
> 	auto hash = [ "abc":1, "def":2, "ghi":3 ];
> 		// Error: non-constant expression ["abc":1,"def":2,"ghi":3]
>
> Seems like a pretty nasty bug to me.
>
>
> T

I would just like to add a vote for using assoc array literals 
outside of function scope. They are a handy way to represent 
data, and often data needs to be defined in other scopes.


More information about the Digitalmars-d-learn mailing list