I was playing around with a hobby project when I came across a type-inference error I didn't understand. I have simplified it to the following trivial example.
I have the following classes and functions:
class Foo { }
class Bar { }
class Baz { }
static T2 F<T1, T2>(Func<T1, T2> f) { return default(T2); }
static T3 G<T1, T2, T3>(Func<T1, Func<T2, T3>> f) { return default(T3); }
Now consider the following examples:
// 1. F with explicit type arguments - Fine
F<Foo, Bar>(x => new Bar());
// 2. F with implicit type arguments - Also fine, compiler infers <Foo, Bar>
F((Foo x) => new Bar());
// 3. G with explicit type arguments - Still fine...
G<Foo, Bar, Baz>(x => y => new Baz());
// 4. G with implicit type arguments - Bang!
// Compiler error: Type arguments cannot be inferred from usage
G((Foo x) => (Bar y) => new Baz());
The last example produces a compiler error, but it seems to me that it should be able to infer the type arguments without any problems.
QUESTION: Why can't the compiler infer <Foo, Bar, Baz>
in this case?
UPDATE: I have discovered that simply wrapping the second lambda in an identity function will cause the compiler to infer all the types correctly:
static Func<T1, T2> I<T1, T2>(Func<T1, T2> f) { return f; }
// Infers G<Foo, Bar, Baz> and I<Bar, Baz>
G((Foo x) => I((Bar y) => new Baz()));
Why can it do all the individual steps perfectly, but not the whole inference at once? Is there some subtlety in the order that the compiler analyses implicit lambda types and implicit generic types?
The lambda cannot be inferred what it's return type is since it is not assigned and cannot be determined by the compiler. Check out this link on how lambdas return types are determined by the compiler. If you would have haved:
then the compiler would have been able to calculate the return type of the lambda based on what it is assigned to, but since now it is not assigned to anything the compiler struggles to determine what the return type for
(Bar y) => new Baz();
would be.For the compiler, a lambda function is distinct from a Func, i.e. using a lambda function for a Func implies a type conversion. The compiler does not do "nested" type conversions when specializing generics. That, however, would be required in your example:
Type of
(Foo x) => (Bar y) => new Baz ()
islambda (Foo, lambda (Bar, Baz))
, butFunc (T1, Func (T2, T3))
would be required, i.e. two conversions, which are nested.Because the algorithm as described in the C# specification doesn’t succeed in this case. Let’s look at the specification in order to see why this is.
The algorithm description is long and complicated, so I’ll heavily abbreviate this.
The relevant types mentioned in the algorithm have the following values for you:
Eᵢ
= the anonymous lambda(Foo x) => (Bar y) => new Baz()
Tᵢ
= the parameter type (Func<T1, Func<T2, T3>>
)Xᵢ
= the three generic type parameters (T1
,T2
,T3
)Firstly, there’s the first phase, which in your case does only one thing:
I’ll skip the details of the explicit parameter type inference here; it suffices to say that for the call
G((Foo x) => (Bar y) => new Baz())
, it infers thatT1
=Foo
.Then comes the second phase, which is effectively a loop that tries to narrow down the type of each generic type parameter until it either finds all of them or gives up. The one important bullet point is the last one:
The output type inference now proceeds as follows; again, only one bullet point is relevant, this time it’s the first one:
The “inferred return type”
U
is the anonymous lambda(Bar y) => new Baz()
andTb
isFunc<T2,T3>
. Cue lower-bound inference.I don’t think I need to quote the entire lower-bound inference algorithm now (it’s long); it is enough to say that it doesn’t mention anonymous functions. It takes care of inheritance relationships, interface implementations, array covariance, interface and delegate co-/contravariance, ... but not lambdas. Therefore, its last bullet point applies:
Then we come back to the second phase, which gives up because no inferences have been made for
T2
andT3
.Moral of the story: the type inference algorithm is not recursive with lambdas. It can only infer types from the parameter and return types of the outer lambda, not lambdas nested inside of it. Only lower-bound inference is recursive (so that it can take nested generic constructions like
List<Tuple<List<T1>, T2>>
apart) but neither output type inferences (§7.5.2.6) nor explicit parameter type inferences (§7.5.2.7) are recursive and are never applied to inner lambdas.Addendum
When you add a call to that identify function
I
:G((Foo x) => I((Bar y) => new Baz()));
then type inference is first applied to the call to
I
, which results inI
’s return type being inferred asFunc<Bar, Baz>
. Then the “inferred return type”U
of the outer lambda is the delegate typeFunc<Bar, Baz>
andTb
isFunc<T2, T3>
. Thus lower-bound inference will succeed because it will be faced with two explicit delegate types (Func<Bar, Baz>
andFunc<T2, T3>
) but no anonymous functions/lambdas. This is why the identify function makes it succeed.