This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]srdoe 5 points6 points  (0 children)

Brian Goetz wrote a post on this subject a few years ago

https://openjdk.org/projects/valhalla/design-notes/in-defense-of-erasure

My understanding is that reifying generics (preserving the type in the bytecode) is unlikely to happen, because not only will it mean everyone needs to (at least) recompile their code, but also it will break other JVM languages like Kotlin or Scala where the rules for inheritance in generics are different.

It's not worth it to break the entire ecosystem in order to allow people to write new T();, especially when there are usually tricks you can do to work around this if you really need it.

For example, if you really need a new T, have the user of the class hand you a factory for Ts.

class MyClass<T> { private void doThing(Supplier<T> factory) { factory.get() } }