To my mind the power of functional purity is when deep code paths can be verified as side-effect free. What are people's experiences in the scale of the code tree that can be inside a pure specifier, and what of the level of code reuse?
A few things I spotted:
std.algorithm
is mostly not marked as pure
, but could potentially be largely pure, either by a pure version of algorithms demanding purity of the instantiating function or mixin, or else by the purity specifier itself being statically polymorphic.
Useful convertors like to!string( someInt )
aren't currently pure.
User defined structs seem to have problems (as illustrated below) with:
1. pure destructors on a nested struct
2. a pure postblit function even on a non-nested struct
The following code currently gives multiple errors on 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'