为什么Java无法推断重载方法中的类型?

时间:2019-08-12 09:28:41

标签: java generics lambda type-inference overload-resolution

我有以下代码,javac无法选择要使用的重载get()方法的版本。为什么不呢?

public class x {
    static <T> T get(Key<T> key, T defaultValue)
    {throw new UnsupportedOperationException();}

    static <T> T get(Key<T> key,
            java.util.function.Supplier<T> defaultValueSupplier)
    {throw new UnsupportedOperationException();}

    static void test(){
        Key<String> k = new Key<>();
        String s = get(k, () -> "test");
    }

    private static class Key<T>{}
}

当我编译时:

% javac -version
javac 1.8.0_202
% javac x.java
x.java:11: error: reference to get is ambiguous
        String s = get(k, () -> "test");
                   ^
  both method <T#1>get(Key<T#1>,T#1) in x and method <T#2>get(Key<T#2>,Supplier<T#2>) in x match
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>get(Key<T#1>,T#1)
    T#2 extends Object declared in method <T#2>get(Key<T#2>,Supplier<T#2>)
x.java:11: error: incompatible types: cannot infer type-variable(s) T
        String s = get(k, () -> "test");
                      ^
    (argument mismatch; String is not a functional interface)
  where T is a type-variable:
    T extends Object declared in method <T>get(Key<T>,T)
2 errors

该错误与Java 9-12版中的错误相同。

我的论点是错误的这一行是错误的:

both method <T#1>get(Key<T#1>,T#1) in x and method <T#2>get(Key<T#2>,Supplier<T#2>) in x match

代码与方法<T#1>get(Key<T#1>,T#1)不匹配,因为k的类型为Key<String>,lambda的类型当然不是String

如果我将代码更改为:

Key<String> k = new Key<>();
Supplier<String> l = () -> "test";
String s = get(k, l);

然后它起作用。是什么导致javac将代码与这两种方法进行匹配?

0 个答案:

没有答案