Could PureAttribute only be guaranteed when manipulating primitive types?

129 Views Asked by At

JetBrains annotations:

Indicates that a method does not make any observable state changes. The same as System.Diagnostics.Contracts.PureAttribute

Microsoft Code Contracts:

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

When operating with primitive types it is very easy to know if your method is a pure function or not.

Consider this scenario however:

public class Program
{
    public static void Main()
    {
        var dodgy = new DodgyClass();

        string.Format("{0}", dodgy);
    }
}

public class DodgyClass
{
    public int State = 0;

    public override string ToString()
    {
        State = new Random().Next();
        return State.ToString();
    }
}

ReSharper tells me that "return value of pure method is not used" which is obviously incorrect since the state does change and in a very visible way. Unless of course they mean that any static method is always pure? Otherwise none of LINQ extension methods are pure since I can do all sort of state manipulation when overriding GetEnumerator().

0

There are 0 best solutions below