Static initialization of associative arrays

H. S. Teoh hsteoh at quickfur.ath.cx
Thu Mar 11 19:12:34 UTC 2021


On Thu, Mar 11, 2021 at 06:06:35PM +0000, Chris Piker via Digitalmars-d-learn wrote:
[...]
> Today I ran across a situation where an immutable associative array
> would be handy. While perusing the language spec I noticed here:
> 
>   https://dlang.org/spec/hash-map.html#static_initialization
> 
> that this feature is not yet implemented.
[...]

The subsequent section on the linked page gives the solution /
workaround. Just declare your immutable AA without initialization, and
initialize it in a static ctor:

	immutable int[string] aa;
	shared static this() {
		aa = [ "abc": 123, "def": 456, /* ... */ ];
	}


T

-- 
INTEL = Only half of "intelligence".


More information about the Digitalmars-d-learn mailing list