在我看来,功能纯度的力量在于可以验证深层代码路径无副作用。人们对可以在纯说明符内的代码树的规模有什么经验,代码重用的水平如何?
我发现了几件事:
std.algorithm
大多数情况下没有标记为pure
,但可能在很大程度上是纯的,或者通过要求实例化函数或 mixin 纯度的纯算法版本,或者纯度说明符本身是静态多态的。
像这样有用的转换器to!string( someInt )
目前并不纯。
用户定义的结构似乎有问题(如下图所示):
1. 嵌套结构上的纯析构函数
2. 即使在非嵌套结构上也有纯 postblit 函数
以下代码目前在 DMD 2.052 win 32-bit 上给出了多个错误
struct InnerStruct
{
pure this(this) {}
pure ~this() {}
}
struct OuterStruct
{
InnerStruct innerStruct;
pure this(this) {}
pure ~this() {}
}
pure void somePureFunc()
{
OuterStruct s1 = OuterStruct(); // pure nested destructor does not compile
OuterStruct s2 = s1;
InnerStruct is1 = InnerStruct(); // pure non-nested destructor seems to compile
InnerStruct is2 = is1; // pure non-nested postblit does not compile
}
void main()
{
somePureFunc();
}
pure_postblit.d(18): Error: pure function 'somePureFunc' cannot call impure function '__cpctor'
pure_postblit.d(20): Error: pure function 'somePureFunc' cannot call impure function '__cpctor'
pure_postblit.d(18): Error: pure function 'somePureFunc' cannot call impure function '~this'
pure_postblit.d(17): Error: pure function 'somePureFunc' cannot call impure function '~this'