1

The PureAttribute documentation says:

Indicates that a type or method is pure, that is, it does not make any visible state changes

  1. Is this the only requirement of a Pure function under in Microsoft Code Contracts?

  2. And; does this model assume that Exceptions are results (as opposed to side-effects)?

I ask because, in a more general context, a pure function also implies that the output is only dependent upon the input; ie. it cannot be the result of I/O or a stochastic function.

One might also argue that a pure function always yields a value to the outer expression, possibly in opposition to Exceptions.

If [Pure] is indeed limited to the less restrictive form, is there an equivalent of a "[FunctionalPure]"?

user2864740
  • 60,010
  • 15
  • 145
  • 220

1 Answers1

2

The static analyser assumes that calling the same pure function with the same arguments twice in a row produces the same result.

Given

[Pure]
public delegate int F(int i);

public class A
{
  public void f(F f)
  {
    var i = f(1);
    Contract.Assert(i == f(1));
  }
}

a warning is generated: "Suggested assumption: Assumption can be proven: Consider changing it into an assert."

So for instance DateTime.Now must not be annoted with the Pure attribute.

As for exceptions, there doesn't seem to be anything disallowing them, nor for requiring them to be thrown consistently. In general, there cannot be. You can always get an OutOfMemoryException for pretty much any code, even a pure function with the same arguments that previously succeeded.