where is the ambiguity here?
The ambiguity here is that the compiler is not trying to output the lambda expression passed to Tuple.Create based on the left side, which already declares the desired type. What happens is a type input algorithm that gets called (no matter how you declare the type of the variable), and you cannot find a suitable match for your lambda expression because it does not have enough information.
This can be easily fixed with a tuple type declaration and an explicit message to the compiler how to output the lambda expression:
t = Tuple.Create<Predicate<int>>(x => true);
If you want to get into the type inference algorithm and see why it does not work:
Given:
Tr M<X1โฆXn>(T1 x1 โฆ Tm xm)
When a method of the form M (E1 ... Em) is called, the type inference task is to search for unique arguments of type S1 ... Sn for each of the parameters of type X1 ... Xn so that M (E1 ... Em) is called.
Now we start:
7.5.2.1 First phase:
For each of the arguments to the Ei method:
If Ei is an anonymous function, the explicit parameter type (ยง7.5.2.7) is inferred from Ei to Ti
So, we will see what makes the explicit type inference:
7.5.2.7 Explicit parameter type expressions
The explicit type of the parameter is derived from the expression E to type T in the following way:
. If E is a clearly expressed anonymous function with parameter types U1 ... Uk and T is a delegate type or an expression tree type with parameter types V1 ... Vk, then for each Ui the exact output (ยง7.5.2.8) is made from Ui to the corresponding Vi.
Your anonymous function is not explicitly typed, so the compiler is not able to draw accurate conclusions from the type of the Ui..Uk parameter to properly overload Tuple.Create .
Yuval Itzchakov
source share