天天看点

d的opApply只是个闭包

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);
    }
}