【发布时间】:2011-08-14 07:22:36
【问题描述】:
在我看来,功能纯度的强大之处在于深度代码路径可以被验证为无副作用。可以在纯说明符内的代码树规模有哪些经验,代码重用程度如何?
我发现了一些东西:
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'
【问题讨论】:
标签: functional-programming d side-effects