title: How to check if a function has no side-effects (is pure) at runtime?
body: <p>So say we have loaded a function <code>F</code> that take in/out a set of <code>args</code> and returns a <code>result</code>. How to check at runtime if this <code>F</code> does not act on anything other than <code>args</code> members and functions? Meaning no <code>Console.Writeline</code>, Singletons (or other stuff not presented in <code>args</code>). Is it possible with <a href="https://github.com/microsoft/CodeContracts" rel="nofollow noreferrer"><code>CodeContracts</code></a> library or some other solution? </p>
  
  <p>Say we know that <code>[Pure]</code> attribute was presented in the function definition. This sucks for many cases when we have a <a href="https://github.com/dotnet/csharplang/blob/master/proposals/lambda-attributes.md" rel="nofollow noreferrer">lambda</a>, yet at least it would be something</p>
  
  <p>Why I do not see how <code>[Pure]</code> can help - this code compiles:</p>
  
  <pre><code>class Test {
      public struct Message {
          public string Data;
      }
  
      public struct Package {
          public int Size;
      }
  
      [Pure]
      public static List&lt;Package&gt; Decomposse(Message m) {
          Console.WriteLine("rrrr"); // This sould not happen
          var mtu = 1400;
          Package p = new Package{Size = mtu};
          return Enumerable.Repeat(p, m.Data.Length / mtu).ToList();
      }
  }
  </code></pre>
  
  <p>And I want to eliminate (or at least detect that function calls stuff like <code>Console.WriteLine("rrrr")</code>)</p>