Method reference is ambiguous for Thread.sleep

前端 未结 3 1696
臣服心动
臣服心动 2021-02-07 08:29

I\'ve come across a weird problem where a method reference to Thread::sleep is ambiguous but a method with the same signature is not.

package test;          


        
3条回答
  •  情书的邮戳
    2021-02-07 09:13

    Personally I see this as some sort of recursion, somehow like this: we need to resolve the method in order to find the target type, but we need to know the target type in order to resolve the method. This has something to do with a special void compatibility rule, but I admit I do not entirely get it.

    Things are even funner when you have something like this:

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

    And try to call it via:

    cool(i -> "Test"); // this will fail compilation 
    

    And btw if you make your lambda explicit, this will work:

    foo((Long t) -> Thread.sleep(t), 1000L);
    

提交回复
热议问题