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代码的数据结构.
浙公网安备 33010602011771号