title: Pure functional programming in D
body: <p>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?</p>
  
  <p>A few things I spotted:</p>
  
  <p><code>std.algorithm</code> is mostly not marked as <code>pure</code>, 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.<br>
  Useful convertors like <code>to!string( someInt )</code> aren't currently pure.</p>
  
  <p>User defined structs seem to have problems (as illustrated below) with:<br>
  1. pure destructors on a nested struct<br>
  2. a pure postblit function even on a non-nested struct  </p>
  
  <p>The following code currently gives multiple errors on DMD 2.052 win 32-bit</p>
  
  <pre class="lang-d prettyprint-override"><code>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();
  }
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>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'  
  </code></pre>