Top level associative arrays

H. S. Teoh via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue May 2 09:34:15 PDT 2017


On Tue, May 02, 2017 at 02:37:20PM +0000, ANtlord via Digitalmars-d-learn wrote:
> On Tuesday, 2 May 2017 at 12:41:01 UTC, Jacob Carlborg wrote:
> > 
> > Note that when declared as "enum", all places it's referenced, a new
> > associative array will be allocated.
> 
> If it is allocated at all places I can move initialization to module
> ctor as says evilrat but how can I make an immutable associative
> array?

Just declare it immutable. The module ctor can still initialize it,
because ctors are allowed to initialize immutables:

------
	immutable string[string] dict;
	static this() {
		dict = [
			"abc": "def",
			"ghi": "lmn"
		];
	}
	void main() {
		import std.stdio;
		writeln(dict["abc"]);
	}
------


T

-- 
Just because you survived after you did it, doesn't mean it wasn't stupid!


More information about the Digitalmars-d-learn mailing list