Method signature selection for lambda expression with multiple matching target types

前端 未结 3 2034
小蘑菇
小蘑菇 2021-02-19 12:03

I was answering a question and ran into a scenario I can\'t explain. Consider this code:

interface ConsumerOne {
    void accept(T a);
}

interface Cust         


        
3条回答
  •  春和景丽
    2021-02-19 12:15

    The code where Eclipse implements JLS §15.12.2.5 does not find either method as more specific than the other, even for the case of the explicitly typed lambda.

    So ideally Eclipse would stop here and report ambiguity. Unfortunately, the implementation of overload resolution has non-trivial code in addition to implementing JLS. From my understanding this code (which dates from the time when Java 5 was new) must be kept to fill in some gaps in JLS.

    I've filed https://bugs.eclipse.org/562538 to track this.

    Independent of this particular bug, I can only strongly advise against this style of code. Overloading is good for a good number of surprises in Java, multiplied with lambda type inference, complexity is quite out of proportion wrt the perceived gain.

提交回复
热议问题