Function and Predicate parameter ambiguous?

前端 未结 1 886
不思量自难忘°
不思量自难忘° 2021-01-05 07:52

Using Java 8, I get a compiler error for the following code:

public class Ambiguous {
    public static void call() {
        SomeDataClass data = new SomeDa         


        
1条回答
  •  情话喂你
    2021-01-05 08:35

    This can be simplified a bit for clarity:

    public static void m(Predicate predicate){
    
    }
    
    public static void m(Function function){
    
    }
    

    And calling it with:

    m(i -> "test")
    

    What do you think will happen? Same thing as in your question.

    Compiler here has to know the method in order to find the target type, but it needs to know the target type in order to resolve the method (it's like a deadlock).

    When you add a cast to Predicate..., you are creating an explicit target type, return type of which is taken into consideration for method overloading as far as I understand.

    0 讨论(0)
提交回复
热议问题