Overload resolution simplification

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Wed Aug 14 14:19:46 PDT 2013

On 14/08/13 20:19, Neal Gafter wrote:
> Maurizio-
> Eric is right: the body of a called method is never involved in 
> overload resolution.  But that has nothing to do with lambdas.
I think I meant a different thing - i.e. the fact that in C# there's 
nothing like this:

<Z> Foo<Z> makeFoo();

Foo<String> fs = makeFoo();

This kind of inference from the target-type doesn't seem to be available 
in C#, right?

If that's the case, in the C# equivalent of the following example:

<U> Target<U> someMethodAcceptingASAM(SAM<U> sam)

Target<String> ts = someMethodAcceptingASAM(x-> ... )

Would C# be able to look at the body of the lambda? My understanding 
(which can be wrong, of course) is that the String on the RHS can't be 
used to unstick the lambda.

> -Neal
> On Wed, Aug 14, 2013 at 3:29 AM, Maurizio Cimadamore 
> <maurizio.cimadamore at oracle.com 
> <mailto:maurizio.cimadamore at oracle.com>> wrote:
>     On 14/08/13 10:57, Maurizio Cimadamore wrote:
>         but it would seems that C# can use the target-type information
>         _ahead_ of overload resolution to perform inference, thus
>         being able to reject applicable candidates based on return
>         mismatch (which Java cannot do).
>     This is incorrect - I found this [1] which clearly states (Eric
>     Lippert) that by design, in C# type information flows from
>     argument types outwards, not the other way around. This means that
>     several examples discussed in this mailing list wouldn't work in
>     C# too, especially the ones around Comparator.comparing - in that
>     case in fact, the only way to type-check the lambda is to
>     instantiate the formal parameter of the method with the
>     information that's available from the target-type context. Java
>     won't do it because that would break the invariant that overload
>     selection should not depend on target type. It seems like C# has a
>     similar (sensible) restriction.
>     [1] -
>     http://stackoverflow.com/questions/3203643/generic-methods-in-net-cannot-have-their-return-types-inferred-why
>     Maurizio

More information about the lambda-spec-observers mailing list