D允许互递归

原文地址
假定在编写模板数组数据结构:

struct Array(T)
{
    T[] store;//后台数组
    int length;//跟踪长度
    void add(T value) {
        if (length == store.length)
            resize(store.empty ? 16 : store.length * 2);//越界即分配.
        store[length++] = value;
    }
    void resize(size_t newsize) {
        Array result;
        result.store = new T[newsize];
        this.store[0 .. length].each!(a => result.add(a));
        this = result;
    }//加倍策略.
}

运行单元测试:

@safe unittest {
    struct S {
        int i;
    }
    Array!S array;
    array.add(S(5));
}

有问题,我们的问题是我们想要@安全,但加/调整默认是@系统,我们void add(T value) @safe,然后继续,我们又写了另一测试:

unittest {
    struct S {
        int i;
        void opAssign(S s) @system { this.i = s.i; }
    }
    Array!S array;
    array.add(S(5));
}

现在@安全是个问题.我们希望编译器在数组中推导何时可用@安全何时需要用@系统.
我们记得模板函数推导属性:

void add()(T value) { ... }
void resize()(size_t newsize) { ... }

不管用,怎么了.问题是add和resize相互递归.D的属性推断在此卡壳了.因为@安全就要检查调整@安全,而调整又要检查@安全.所以它放弃了,直接@系统了.
如何解决?我们定义个普通函数:

alias trial = {
    auto store = new T[1];
    store[0] = T.init;
};

该函数代表T中的互递归.它尝试我们需要操作并告诉我们哪些属性是安全的.由于是λ,也可推导属性,且不参与任何递归,因而避免了该问题.然后只需转移属性到我们的递归对.

struct Array(T)
{
    T[] store;
    int length;
    alias trial = {
        auto store = new T[1];
        store[0] = T.init;
    };
    enum attributes = [__traits(getFunctionAttributes, trial)].join(" ");
    mixin(q{
        void add(T value)} ~ attributes ~ q{
        {
            if (length == store.length)
                resize(store.empty ? 16 : store.length * 2);
            store[length++] = value;
    	}
        void resize(size_t newsize)} ~ attributes ~ q{
        {
            Array result;
            result.store = new T[newsize];
            this.store[0 .. length].each!(a => result.add(a));
            this = result;
        }
    });
}

现在,我们在互递归中手动指定每个函数属性,因此D不必再推导,从而产生适用于@safe@system代码的数据结构.

posted @   zjh6  阅读(14)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示