On 2/16/20 7:57 AM, AlphaPurned wrote:
template AA(string[] S)
{
     auto _do() { int[string] d; foreach(s; S) d[s] = 0; return d; }
     enum AA = _do;

evilrat is 100% correct. Each time you call that if statement, it constructs a NEW AA, checks to see if the t parameter is in there, and then leaves it for garbage.

In order to fix it, you have to initialize it in a shared static constructor (D doesn't currently allow static initialization of runtime AAs).

example:

template AA(string[] S)
{
    __gshared const int[string] AA;
    enum _do = (){ int[string] d; foreach(s; S) d[s] = 0; return d; }();
    shared static this()
    {
        AA = _do;
    }
}

-Steve

Reply via email to