< T扩展Enum< T>> T []作为Java 8中的返回类型
我正在查看一些在java 7中编译的代码,但是没有在Java 8中编译。
I am looking at some code which compiles in java 7 but fails to compile in Java 8.
class Test {
public <T extends Enum<T>> T[] doSomething(...) {...}
public <T extends Enum<T>> T[] methodWhichCallsDoSomething() {
...
return doSomething(...);
}
错误如下:
The error is as follows:
incompatible types: inference variable T has incompatible upper bounds java.lang.Enum<T>,T
我正在运行jdk 1.8.0_71。
I am running jdk 1.8.0_71.
有人知道一个解决方法或解决方案来获得它在Java 8中的编译和工作吗?
Does anyone know a workaround or solution for getting this to compile and work in Java 8?
编辑:错误消息是针对调用doSomething()的行而提供的,只是括号之前的列。
The error message is given for the line calling doSomething(), for the col just before the parenthesis.
通过你传递给 doSomething
的东西。你没有发布实际的参数,但是从签名我们可以推断出,如果至少有一个 doSomething
的参数引用 T
以任何方式, methodWhichCallsDoSomething
不可能提供正确的参数(除非所有都是 null
),因为在范围内没有适当的变量。
The problem is caused by what you are passing to doSomething
. You didn’t post the actual arguments, but from the signature we can deduce that, if at least one of doSomething
’s parameters refers to T
in any way, it is impossible for methodWhichCallsDoSomething
to provide correct arguments (unless all are null
), as no appropriate variable is in scope.
由于编译器错误或未经检查的操作,代码编译的可能性非常大,无论是哪种情况,这种行为确实有可能是由于新的类型推断规则而改变的。
It’s very likely that the code compiled either, due to a compiler error or an unchecked operation, and in either case, it is indeed possible that this behavior has changed due to the new type inference rules.
无参数方法,如 methodWhichCallsDoSomething()
承诺返回任何 T []
来电者的意愿,无论如何都会被打破。有几个类似的反模式的例子,现在在Java 8下失败,例如这个和this 。如果这样的代码碰巧在早期版本中工作,那么偶然会发生这种情况,现在发生的问题实际上已经存在。
A parameter-less method like methodWhichCallsDoSomething()
promising to return whatever T[]
the caller wishes, is broken anyway. There are several examples for similar anti-patterns, failing now under Java 8, like this and this. If such code happened to work in earlier version, it happened to work by accident and the problem occurring now were actually already there before.
对于这样的破解代码,获取编译器现在出现错误,可能出现的症状好一些,代码仍在编译,但会在运行时突然中断。
For such broken code, getting a compiler error now, is the better of the possible symptoms as sometime, the code still compiles, but will suddenly break at runtime.