简体   繁体   English

JDK11 javac类型推断错误信息解读

[英]Interpretation of JDK11 javac type inference error message

I'm having a hard time understanding the error message below:我很难理解下面的错误消息:

Program.java:67: error: incompatible types: inferred type does not conform to lower bound(s)
       .map(result ->
           ^
   inferred: CAP#1
   lower bound(s): Document<?>,CAP#1
 where CAP#1 is a fresh type-variable:
   CAP#1 extends Document<?> from capture of ?

  • Are there two lower bounds or the lower bound is just Document<?> where ?是否有两个下限或下限只是Document<?> where ? is transformed to CAP#1 ?转换为CAP#1
  • What's a fresh type-variable?什么是fresh的类型变量?
  • CAP#1 extends Document<?> from capture of? what is the meaning of the from capture of? from capture of?
  • Is the problem here that one lower bound was inferred but two are required or something else?这里的问题是推断出一个下限但需要两个下限还是其他什么?
  • Is the error because CAP#1 extends Document<?> isn't a lower bound to Document<?> ?错误是因为CAP#1 extends Document<?>不是 Document<?> 的下限Document<?>

PS: I omitted the code because right now my problem is trying to understand what this message is saying. PS:我省略了代码,因为现在我的问题是试图理解这条消息在说什么。

As best I can without reference to the relevant code, and taking the questions in a different order:尽我所能不参考相关代码,并以不同的顺序提出问题:

  • What's a fresh type-variable?什么是fresh的类型变量?

It is a synthetic type variable that the type analyzer invented in the process of analyzing the same statement for which the diagnostic is reported.它是类型分析器在分析报告诊断的同一语句的过程中发明的合成类型变量。 Contrast with a regular type variable or a synthetic type variable arising from the analysis of some preceding statement.与通过分析前面的某些语句产生的常规类型变量或合成类型变量进行对比。

  • CAP#1 extends Document<?> from capture of? what is the meaning of the from capture of? from capture of?

Not much.不多。 It's talking about which type expression's value is represented by CAP#1 , but that's not very informative in this case.它讨论的是CAP#1表示的类型表达式的值,但在这种情况下,这并不是很丰富。

  • Are there two lower bounds or the lower bound is just Document<?> where ?是否有两个下限或下限只是Document<?> where ? is transformed to CAP#1 ?转换为CAP#1

I interpret the message to be saying that there are two lower bounds with which CAP#1 must comply.我将消息解释为CAP#1必须遵守两个下限。 One is Document<?> and the other is CAP#1 .一个是Document<?> ,另一个是CAP#1

  • Is the problem here that one lower bound was inferred but two are required or something else?这里的问题是推断出一个下限但需要两个下限还是其他什么?

Something else.别的东西。 There's nothing inherently wrong with type analysis determining that there are multiple lower bounds with which a given type must comply.类型分析确定给定类型必须遵守多个下限并没有本质上的错误。

  • Is the error because CAP#1 extends Document<?> isn't a lower bound to Document<?> ?错误是因为CAP#1 extends Document<?>不是 Document<?> 的下限Document<?>

No. As a whole, CAP#1 extends Document<?> is not a type expression, so the question of whether it may be a lower type bound to anything does not make sense.不。作为一个整体, CAP#1 extends Document<?>不是类型表达式,因此它是否可能是绑定到任何东西的较低类型的问题没有意义。 Rather, the message seems to be saying that the compiler's analysis shows that CAP#1 is required to be a subtype of Document<?> .相反,该消息似乎是在说编译器的分析表明CAP#1必须是Document<?>的子类型。 This part of the message is just extra information.这部分消息只是额外的信息。 The description of the actual nature of the error is what comes before.错误的实际性质的描述是前面的内容。

It's hard to be sure what the actual issue is.很难确定实际问题是什么。 It does appear that the type analyzer has determined that the captured type is its own lower bound, but that's not necessarily a problem, as that constraint is trivially satisfied.看起来类型分析器已经确定捕获的类型是它自己的下限,但这不一定是一个问题,因为该约束很容易得到满足。 That does not, however, assist the compiler in capturing a specific type.但是,这并不能帮助编译器捕获特定类型。

I suspect that the crux of the issue is that Document<?> is both an upper bound and (especially) a lower bound to the captured type.我怀疑问题的症结在于Document<?>是捕获类型的上限和(尤其是)下限。 It is nonsensical to have a lower bound expression with a wildcard type parameter, because no actual type can satisfy that.有一个带有通配符类型参数的下限表达式是没有意义的,因为没有实际类型可以满足它。 In that case, the "does not conform to lower bound(s)" message is a bit misleading, but not incorrect.在这种情况下,“不符合下限”消息有点误导,但并非不正确。 If this is indeed the problem then a clearer message would be along the lines of "lower bound(s) cannot be satisfied".如果这确实是问题,那么更清晰的信息将是“无法满足下限”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM