简体   繁体   中英

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 ? is transformed to CAP#1 ?
  • What's a fresh type-variable?
  • CAP#1 extends Document<?> from capture of? what is the meaning of the 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<?> ?

PS: I omitted the code because right now my problem is trying to understand what this message is saying.

As best I can without reference to the relevant code, and taking the questions in a different order:

  • What's a fresh type-variable?

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?

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.

  • Are there two lower bounds or the lower bound is just Document<?> where ? is transformed to CAP#1 ?

I interpret the message to be saying that there are two lower bounds with which CAP#1 must comply. One is Document<?> and the other is 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<?> ?

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. Rather, the message seems to be saying that the compiler's analysis shows that CAP#1 is required to be a subtype of 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. 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".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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