3 ms·
Erasure 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 clas
by kmiroslav 10y ago
Erasure 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(); }