Question

    public static Func<string, Task<T>> MyMethod<T>(
        UserCredentials credentials,
        Func<string, string, string, Task<T>> func
    ) =>
        async (value) => await func(credentials.user, credentials.pwd, value);

I'm having difficulty discerning the purity of the function, because obviously T(int) results in a different output as T(string). Or does the purity depend on T(int) = 5 always resulting in 10 and T(string) = "Foo" always resulting in "Bar"?

Was it helpful?

Solution

Or does the purity depend on T(int) = 5 always resulting in 10 and T(string) = "Foo" always resulting in "Bar"?

Yes.

A Type Parameter is still one of the function's input parameters, even though it doesn't reside within the function's parentheses. The definition of a pure function is that, given the same values for its input parameters, it will still produce the same result (without any side-effects).

OTHER TIPS

Using the definition of Pure Function from wikipedia, a function is pure if:

  1. The function always evaluates the same result value given the same argument value(s). The function result value cannot depend on any hidden information or state that may change while program execution proceeds or between different executions of the program, nor can it depend on any external input from I/O devices (usually—see below).
  2. Evaluation of the result does not cause any semantically observable side effect or output, such as mutation of mutable objects or output to I/O devices (usually—see below).

There are two ways I think you can look at this: 1. the type parameter T is part of the function definition or it's part of the input to the function. Either way, I don't think it matters in determining whether the function is pure.

Licensed under: CC-BY-SA with attribution
scroll top