import std.stdio;
struct A {
static int[string] impl;
static this() {
impl = [
"a": 1,
"b": 2,
];
}

int opApply(scope int delegate(string a, int b) dg) {//实际执行的是这个函数.
foreach (k, v; impl) {
auto r = dg(k, v);//插入闭包.
if (r) return r;
}
return 0;
}
}

void main() {
A a;
foreach (k, v; a) {
writefln("%s -> %s", k, v);
}
}

​d​​​的​​opApply​​​只是​​官方​​​的一个​​稍微​​​高阶的​​闭包​​​罢了.​​每一​​​的​​{}​​​作为闭包内容,相当于​​策略类​​​插入到​​opApply​​​的​​基础类​​​里面!是不是​​策略类​​到处都有?

应该这样理解,​​类​​​是​​插件​​​.把​​类​​​当作​​{}​​​块.插入​​基块​​​中.而不是​​函数​​​作​​插件​​​,​​函数​​​作插件没有​​类​​​作插件方便,舒服!
而真正的​​​函数​​​与​​类​​​的公共基块应该是​​{}​​​块.以​​{}​​​为​​基块​​​,当然​​编译​​​过程中就是这样处理的.​​{}​​为基块.