java - JLS 的哪一部分指定您不能从 List<?将 List<Superclass>> 扩展到 List<List<Subclass>>?

标签 java language-lawyer

(这个问题的灵感来自 this question,我回答错了。)
此代码无法编译:

List<? extends List<Number>> list = new ArrayList<>();
List<List<Double>> anotherList = (List<List<Double>>) list;
请注意,IntelliJ 不会报告任何错误。只有当我单击“运行”时它才会编译失败。
我理解为什么这不能在概念层面上编译。 list是“扩展 List<Number> 的东西”的列表,并且“某物”永远不可能是 List<Double> , 因为 List<Double>不是 List<Number> 的子类型, 并且因为它们具有相同的删除,所以没有类型可以实现两者。
然而,当我试图按照语言规范中的措辞来确定这个转换是否有效时,我发现语言规范似乎说这是一个有效的转换!
这是我的推理:
转换满足所有三个要求,以便从 S 进行缩小引用转换( List<? extends List<Number>> ) 到 T (List<List<Double>>)。

5.1.6.1. Allowed Narrowing Reference Conversion

A narrowing reference conversion exists from reference type S to reference type T if all of the following are true:

  • S is not a subtype of T

  • If there exists a parameterized type X that is a supertype of T, and a parameterized type Y that is a supertype of S, such that the erasures of X and Y are the same, then X and Y are not provably distinct (§4.5).

  • One of the following cases applies:

    • S and T are interface types.
    • [...]

第一点和第三点是微不足道的。为了证明第二点是正确的,我们取 Collection<List<Double>>成为 List<List<Double>> 的参数化父类(super class)型, 和 Collection<? extends List<Number>>成为 List<? extends List<Number>> 的参数化父类(super class)型.它们都删除为相同的类型Collection .现在我们需要证明 Collection<? extends List<Number>>Collection<List<Double>>不是 provably distinct (§4.5) .同样的论点也适用于 Iterable<...> .
编辑:我刚刚意识到 List<List<Double>> 的父类(super class)型还包括 List<? extends List<Double>> 之类的内容,而不仅仅是 List 的 super 接口(interface).但我认为这不会使这个论点无效,因为关键是 1. out of XY至少有一个通配符 2. X 的通配符边界/类型参数和 Y是彼此的亚型。

Two parameterized types are provably distinct if either of the following is true:

  • They are parameterizations of distinct generic type declarations.

  • Any of their type arguments are provably distinct.


显然,由于它们都删除为相同的类型,因此第一个条件不可能为真。我们只需要证明第二个条件是假的。
§4.5.1 ,规范定义“类型参数可证明是不同的”:

Two type arguments are provably distinct if one of the following is true:

  • [...]
  • One type argument is a type variable or wildcard, with an upper bound (from capture conversion (§5.1.10), if necessary) of S; and the other type argument T is not a type variable or wildcard; and neither |S| <: |T| nor |T| <: |S| (§4.8, §4.10).

(为简洁起见,未显示其他(错误的)条件)这里,SList<Number>TList<Double> .两者|S| <: |T|和 |T| <: |S|成立,如 |S|和 |T|是同一类型。是的,子类型关系是自反的,因为父类(super class)型关系是自反的(它被定义为直接父类(super class)型关系的自反和传递闭包)。
因此 Collection<? extends List<Number>> 的类型参数和 Collection<List<Double>>无法证明是不同的,因此 List<? extends List<Number>>List<List<Double>>无法证明是不同的,因此存在(或应该存在)从 List<? extends List<Number>> 的转换至List<List<Double>> !
我的推理错误在哪里?我错过了规范的其他部分吗?

最佳答案

好吧,我就在Holger's confirmation and answer之后再说吧: JLS 在这个位置没有被指定(至少)。有一些相关的 JDK 错误可以解决相同的想法,值得注意 this one ,直接通过以下方式解决您的问题:

.... Otherwise, map wildcards and type variables to their upper bounds, and then test whether their erasures are related classes or interfaces (that is, one erased type is a subtype of the other)


只是为了立即开始下一句:

This is unsound...


因此,该错误承认 JLS 需要围绕本章进行一些更正。
从你对 JLS 的引用中,我也一直在努力解决两点:
  • 一个类型参数是类型变量或通配符,其上限为 (如有必要,来自捕获转换(§5.1.10)) ...
    我确实知道什么是捕获转换,但我不知道它可能是可选的(通过“如有必要”)。我一直认为它一直在每个位置进行。
  • 捕获的转化类型的上限是多少?
    在您的情况下,上限是 List<Number>List<?> , 例如?以我的理解(或缺乏精确的 JLS 解释),这可以理解为任何一种方式。

  • 所有这些(+你对 JLS 的大刮)让我想知道这里 JLS 的正确性,尤其是因为 javac没有遵循这些完全相同的规则。

    关于java - JLS 的哪一部分指定您不能从 List<?将 List<Superclass>> 扩展到 List<List<Subclass>>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66075419/

    相关文章:

    java - 正则表达式

    java - 解析日期是 future 几分钟

    java - Jackson 序列化和反序列化日期时间从/到 WCF 日期时间

    c++ - 为什么不修改关联容器的键?

    c++ - 为什么平凡的可复制类要求析构函数必须平凡

    c - %% 应该跳过 scanf 中的前导空格吗?

    java - Kafka : Single consumer group in multiple instances

    java广义超几何函数

    c++ - 非限定名称查找找到内联命名空间成员

    c++ - 函数参数的默认参数是否被视为该参数的初始值设定项?