[英]Why can't Java 7 diamond operator be used with anonymous classes?
Consider this Java code which attempts to instantiate some List
s:考虑这个 Java 代码,它试图实例化一些
List
:
List<String> list1 = new ArrayList<String>();
List<String> list2 = new ArrayList<>();
List<String> list3 = new ArrayList<String>() { };
List<String> list4 = new ArrayList<>() { };
List<String> list5 = new ArrayList<Integer>() { };
list1
and list2
are straightforward; list1
和list2
很简单; list2
uses the new diamond operator in Java 7 to reduce unnecessary repetition of the type parameters. list2
使用 Java 7 中新的菱形运算符来减少不必要的类型参数重复。
list3
is a variation on list1
using an anonymous class, potentially to override some methods of ArrayList
. list3
是list1
的变体,使用匿名 class,可能会覆盖ArrayList
的某些方法。
list4
attempts to use the diamond operator, similar to list2
, but this is a compile error, with the message '<>' cannot be used with anonymous classes . list4
尝试使用类似于list2
的菱形运算符,但这是一个编译错误,消息为'<>' cannot be used with anonymous classes 。
list5
produces an error that proves the compiler knows what type is actually needed. list5
产生一个错误,证明编译器知道实际需要什么类型。 The error message is Type mismatch: cannot convert from new ArrayList<Integer>(){} to List<String>错误信息是Type mismatch: cannot convert from new ArrayList<Integer>(){} to List<String>
So, with the declaration of list4
, why can't the diamond operator be used with anonymous classes?那么,有了
list4
的声明,为什么菱形运算符不能与匿名类一起使用呢? There is a similar question here with an accepted answer that contains the following explanation from JSR-334 :这里有一个类似的问题,接受的答案包含来自JSR-334的以下解释:
Using diamond with anonymous inner classes is not supported since doing so in general would require extensions to the class file signature attribute to represent non-denotable types, a de facto JVM change.
不支持将菱形与匿名内部类一起使用,因为这样做通常需要扩展 class 文件签名属性以表示不可表示的类型,这是事实上的 JVM 更改。
I need some help understanding that reasoning.我需要一些帮助来理解这个推理。 Why would an explicit type versus the identical and apparently easily inferred type require any difference in the resulting class file?
为什么显式类型与相同且显然很容易推断的类型需要在生成的 class 文件中有任何差异? What difficult use case would be covered by "doing so in general"?
“通常这样做”将涵盖哪些困难的用例?
This was discussed on the "Project Coin" mailing list .这在“Project Coin”邮件列表中进行了讨论。 In substance (emphasis mine):
实质上(强调我的):
Internally, a Java compiler operates over a richer set of types than those that can be written down explicitly in a Java program.
在内部,Java 编译器操作的类型比可以在 Java 程序中显式写下的类型更丰富。 The compiler-internal types which cannot be written in a Java program are called non-denotable types.
不能在 Java 程序中编写的编译器内部类型称为不可表示类型。 Non-denotable types can occur as the result of the inference used by diamond.
作为钻石使用的推理的结果,可能会出现不可表示的类型。 Therefore, using diamond with anonymous inner classes is not supported since doing so in general would require extensions to the class file signature attribute to represent non-denotable types, a de facto JVM change .
因此,不支持将菱形与匿名内部类一起使用,因为这样做通常需要对类文件签名属性进行扩展以表示不可表示的类型,这是事实上的 JVM 更改。 It is feasible that future platform versions could allow use of diamond when creating an anonymous inner class as long as the inferred type was denotable.
只要推断的类型是可表示的,未来的平台版本就可以在创建匿名内部类时允许使用菱形。
Note that it is not supported in Java 8 either but will be included as a new feature in Java 9 (Item 3 of "Milling Project Coin" ).请注意,它在 Java 8 中也不支持,但将作为 Java 9 中的一项新功能包含在内( “Milling Project Coin”的第 3 项)。
You can use diamond opeator in Java9您可以在Java9 中使用菱形运算符
MyHandler<Integer> intHandler = new MyHandler<>(1) {
@Override
public void handle() {
// handling code...
}
};
MyHandler<? extends Integer> intHandler1 = new MyHandler<>(10) {
@Override
void handle() {
// handling code...
}
};
MyHandler<?> handler = new MyHandler<>("One hundred") {
@Override
void handle() {
// handling code...
}
};
}
You can use it in java 9 Example Diamond operator您可以在 java 9 Example Diamond 运算符中使用它
MyHandler<Integer> intHandler = new MyHandler<>(1) {
@Override
public void handle() {
// handling code...
}
};
From Java 10 , you can do easily with var
, the compiler will take care of the Type inference.从Java 10 开始,您可以使用
var
轻松完成,编译器将负责类型推断。
var list1 = new ArrayList();
var list2 = new ArrayList<String>();
var list3 = new ArrayList<String>() { };
var list4 = new ArrayList<>() { };
var list5 = new ArrayList<Integer>() { };
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.