Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type arguments can't be inferred from the usage for higher-order function

I have the following higher-order function:

public static Func<T, bool> Not<T>(Func<T, bool> otherFunc)
{
    return arg => !otherFunc(arg);
}

And trying to call it like that:

var isValidStr = LinqUtils.Not(string.IsNullOrWhiteSpace);

Compiler gives me "type arguments cannot be inferred from the usage" error. But the following works:

var isValidStr = LinqUtils.Not((string s) => string.IsNullOrWhiteSpace(s));

I wonder what the difference is? string.IsNullOrWhiteSpace is already a non-overloaded function with the exactly same signature.

As mentioned in comments, the following also works and still doesn't explain why type inference fails in this case:

var isValidStr = LinqUtils.Not<string>(string.IsNullOrWhiteSpace);
like image 240
Grozz Avatar asked Jun 12 '12 17:06

Grozz


1 Answers

The details of the issue you are dealing with are answered by Eric Lippert on his blog, here.

Basically, as "David B" said in your comments, "IsNullOrWhiteSpace is a method group. The method group only has one participating member today, but it could get more in the future."

like image 113
payo Avatar answered Oct 21 '22 18:10

payo