Why can't diamond infer types on anonymous inner classes?

后端 未结 3 1205
别那么骄傲
别那么骄傲 2020-12-28 16:53

In Java 7 and later, diamond can be used to infer types on normally like so without an issue:

List list = new ArrayList<>();
3条回答
  •  佛祖请我去吃肉
    2020-12-28 17:49

    In the 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.

    What I guess is that as everybody knows, anonymous class leads to a generation of its own class file.

    I imagine that generic type doesn't exist within these files and rather replaced by the effective (static) type (thus declared by the explicit type like at declaration object time).

    Indeed, file corresponding to an inner class is never shared across multiple different instantiations of it, so why bother with generics into it?! :).

    It would be more hardly achievable (and surely useless) for compiler to force an extension (by adding a special attribute for generics) to theses kind of class files.

提交回复
热议问题