Why can't the C# compiler infer T to int in the specified example?
void Main()
{
int a = 0;
Parse("1", x => a = x);
// Compiler error:
// Cannot convert expression type 'int' to return type 'T'
}
public void Parse<T>(string x, Func<T, T> setter)
{
var parsed = ....
setter(parsed);
}
See http://msdn.microsoft.com/en-us/library/ms379564%28v=vs.80%29.aspx section on generic methods.
Method type inference on a lambda requires that the types of the lambda parameters be already known before the types of the returns are inferred. So for example if you had:
and a call
then we would infer:
See, we need A to work out B, and B to work out C. In your case you want to work out T from... T. And you can't do that.