d编译时与运行时关联数组

enum string[string] CtfeFoo = ["foo" : "bar"];
static immutable string[string] Foo;

static this()
{
    Foo = CtfeFoo;
}

string ctfeableFunction()
{
    if (__ctfe)
    	return CtfeFoo["foo"];
    else
        return Foo["foo"];
}

void main()
{
    enum a = ctfeableFunction();
    auto b = ctfeableFunction();
}
posted @ 2020-01-19 07:47  zjh6  阅读(13)  评论(0)    收藏  举报  来源