www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Initialize associate array

reply pham <home home.com> writes:
Should codes below be compiled?

import std.stdio;

class Test
{
	enum string[string] WorkAA =
	[
		"foo": "work"
	];

	immutable string[string] NotWorkAA1 =
	[
		"foo": "not work"
	];

	string[string] NotWorkAA2 =
	[
		"foo": "not work"
	];
}

void main()
{
	Test t = new Test();	
}

Test with DPaste with below error messages

Result: Compilation error / Return code: 1 (Hangup)
Compilation output:

/d699/f49.d(11): Error: non-constant expression ["foo":"not work"]
/d699/f49.d(16): Error: non-constant expression ["foo":"not work"]
Feb 29 2016
next sibling parent cym13 <cpicard openmailbox.org> writes:
On Monday, 29 February 2016 at 21:03:46 UTC, pham wrote:
 Should codes below be compiled?

 import std.stdio;

 class Test
 {
 	enum string[string] WorkAA =
 	[
 		"foo": "work"
 	];

 	immutable string[string] NotWorkAA1 =
 	[
 		"foo": "not work"
 	];

 	string[string] NotWorkAA2 =
 	[
 		"foo": "not work"
 	];
 }

 void main()
 {
 	Test t = new Test();	
 }

 Test with DPaste with below error messages

 Result: Compilation error / Return code: 1 (Hangup)
 Compilation output:

 /d699/f49.d(11): Error: non-constant expression ["foo":"not 
 work"]
 /d699/f49.d(16): Error: non-constant expression ["foo":"not 
 work"]
According to the specs it should but right now it doesn't, it's a know long-lasting bug. AA can't be initialized at compile-time for the moment (and likely not before a long time).
Feb 29 2016
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
Use a constructor instead.
Feb 29 2016