5 ms·
For me, it's mainly that you know the runtime type of the type parameter, so you can do things like new T() in your generic class.
by adevine 10y ago
For me, it's mainly that you know the runtime type of the type parameter, so you can do things like new T() in your generic class.
- kmiroslav 10y agoPass a factory `createT()` instead, problem solved.
- adevine 10y agoExactly - except it's gross and results in a lot of annoying boilerplate code for what should be considered a simple operation. Tons of generic APIs essentially do the same thing by passing in the class object in the constructor, e.g. http://stackoverflow.com/a/1090488/1075909 http://stackoverflow.com/a/1090488/1075909 . The question for people not that familiar with the Java type system is often "Why do I have to pass in the class object when I just declared the type parameter in my instance?" That's not the only thing you need to workaround because of type erasure (the "TypeToken" stuff in a lot of deserialization libraries is another one that comes to mind frequently), but it's probably the first annoying example people hit.
- kmiroslav 10y agoErasure is not the problem here, you need to be able to specify a type constraint that will allow you to invoke T(). What if T is an interface? An abstract class? A class with no default constructor? The only reasonable way to do this is to accept a lambda that tells the compiler exactly what call is legal to create an instance of T. And that's called a factory.
- adevine 10y agoYes, good point. But if you did have a type constraint, like C#, you would still need the runtime type info to know what to create. This is legal in C#: public static T Factory<T>() where T:new() { return new T(); }