I'm trying to write a complement function, such that when provided with a function f, it returns a function which, when provided with the same input as f, returns it's logical opposite.
Having put similar code into VS2017, I get no errors, however I'm not yet able to run the code to see if it'll work as expected. My intention was to try this in a repl first, to see if it would do as expected. The code I used there was this:
public static Func<T, bool> Complement<T>(Func<T, bool> f) { return (T x) => !f(x); } public static bool GreaterThanTwo (int x) { return x > 2; } static public void Main(string[] args) { Func<int, bool> NotGreaterThanTwo = Complement(GreaterThanTwo); Console.WriteLine(NotGreaterThanTwo(1)); } Within the repl, I get the error:
main.cs(17,42): error CS0411: The type arguments for method `MainClass.Complement(System.Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly Compilation failed: 1 error(s), 0 warnings compiler exit status 1
I have looked at a few questions on stack overflow which cover the same error message, for instance this and this, but I'm not able to see how they relate to this issue I'm having.
Complement(GreaterThanTwo);toComplement<int>(GreaterThanTwo);Complement.Complement(GreaterThanTwo)is trying to use a method group, not aFunc<int,bool>, which is what confuses the compilerFunc<T, bool>e.g.Func<int, bool> gt = GreaterThanTwo; var NotGreaterThanTwo = Complement(gt);the code compilesComplement((Func<int, bool>)GreaterThanTwo)