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

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

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

posted @ 2022-10-25 10:45  zjh6  阅读(14)  评论(0)    收藏  举报  来源