匹配捕获与上限时类型检查中断?
Type checking broken on matching capture with upper bound?
创建一个class 赞
public class Play {
public static void main(String[] args) throws Exception {
outer(Integer.class, inner("abc"));
}
static <C> void outer(Class<C> c, List<? super C> s){
}
static <C> List<C> inner(C c) {
return null;
}
}
它在 Java 8 中编译! (在 Eclipse 4.5 和 JDK1 中。8_25)https://ideone.com/Q9JLHP
在 Eclipse 中,所有边界都被正确推断,但是 outer
的捕获 Supplier<? super Integer>
怎么可能被参数 Supplier<String>
满足??
编辑:澄清这是 Java 8 特有的,并使示例不那么混乱。
根据编译器的判断,inner("abc")
可以解释为 String
的任何超类型的 Supplier
。 -- 例如,
Supplier<Object> inner = inner("abc");
工作正常,因为 "abc"
是 也是 和 Object
。这就是这里发生的事情:inner
返回给你 Supplier<Object>
.
inner
的推论要求 C
是 Integer
和 String
的超类型。
C
到底是什么?那是一个复杂的故事。当然,Integer
和 String
都是 Object
。但两者都是Serializable
!还有Comparable<?>
....
到头来,这并不重要;我们需要知道的是它是 String
和 Integer
的 "least upper bound",无论如何它是 defined.
创建一个class 赞
public class Play {
public static void main(String[] args) throws Exception {
outer(Integer.class, inner("abc"));
}
static <C> void outer(Class<C> c, List<? super C> s){
}
static <C> List<C> inner(C c) {
return null;
}
}
它在 Java 8 中编译! (在 Eclipse 4.5 和 JDK1 中。8_25)https://ideone.com/Q9JLHP
在 Eclipse 中,所有边界都被正确推断,但是 outer
的捕获 Supplier<? super Integer>
怎么可能被参数 Supplier<String>
满足??
编辑:澄清这是 Java 8 特有的,并使示例不那么混乱。
inner("abc")
可以解释为 String
的任何超类型的 Supplier
。 -- 例如,
Supplier<Object> inner = inner("abc");
工作正常,因为 "abc"
是 也是 和 Object
。这就是这里发生的事情:inner
返回给你 Supplier<Object>
.
inner
的推论要求 C
是 Integer
和 String
的超类型。
C
到底是什么?那是一个复杂的故事。当然,Integer
和 String
都是 Object
。但两者都是Serializable
!还有Comparable<?>
....
到头来,这并不重要;我们需要知道的是它是 String
和 Integer
的 "least upper bound",无论如何它是 defined.