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:
7.5.2.1 The first phase
For each of the method arguments Eᵢ
(in your case, there’s only one, the lambda):
- If
Eᵢ
is an anonymous function [it is], an explicit parameter type inference (§7.5.2.7) is made from Eᵢ
to Tᵢ
- Otherwise, [not relevant]
- Otherwise, [not relevant]
- Otherwise, no inference is made for this argument.
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 that T1
= 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:
7.5.2.2 The second phase
The second phase proceeds as follows:
- [...]
- Otherwise, for all arguments
Eᵢ
with corresponding parameter type Tᵢ
where the output types (§7.5.2.4) contain unfixed type variables Xj
but the input types (§7.5.2.3) do not, an output type inference (§7.5.2.6) is made from Eᵢ
to Tᵢ
. Then the second phase is repeated.
[Translated and applied to your case, this means:
- Otherwise, if the return type of the delegate (i.e.
Func<T2,T3>
) contains an as yet undetermined type variable (it does) but its parameter types (i.e. T1
) do not (they do not, we already know that T1
= Foo
), an output type inference (§7.5.2.6) is made.]
The output type inference now proceeds as follows; again, only one bullet point is relevant, this time it’s the first one:
7.5.2.6 Output type inferences
An output type inference is made from an expression E
to a type T
in the following way:
- If
E
is an anonymous function [it is] with inferred return type U
(§7.5.2.12) and T
is a delegate type or expression tree type with return type Tb
, then a lower-bound inference (§7.5.2.9) is made from U
to Tb
.
- Otherwise, [rest snipped]
The “inferred return type” U
is the anonymous lambda (Bar y) => new Baz()
and Tb
is Func<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:
- Otherwise, no inferences are made.
Then we come back to the second phase, which gives up because no inferences have been made for T2
and T3
.
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 in I
’s return type being inferred as Func<Bar, Baz>
. Then the “inferred return type” U
of the outer lambda is the delegate type Func<Bar, Baz>
and Tb
is Func<T2, T3>
. Thus lower-bound inference will succeed because it will be faced with two explicit delegate types (Func<Bar, Baz>
and Func<T2, T3>
) but no anonymous functions/lambdas. This is why the identify function makes it succeed.
(Bar y) => new Baz()
toFunc<Bar, Baz>
(and even drop theBar
fromBar y
) and it will compile. – Yettie