One thing that irritates me about Java is the awful implementation of compile-time translation of generic type arguments.
I can observe and understand that the C# implementation is far better, but I’m confused as to how it works.
Essentially, how can you say:
T t = new T()
If you don’t know the type of T and therefore don’t know the constructor argument requirements?
I can see
Class<T> cl = T.class
or
T[] tarr = new T[0]
but I don’t see how you can really create a new instance of T if you don’t know the requirements of constructing it?
You can only do
new T();if T is constrained to have a plain, parameterless public constructor, for instance:Additionally, there is no way to specify that any other sort of constructor exist. This is not possible:
To your other points, this is how you get the type of T at runtime:
and creating an array of
Tdoesn’t actually create any instances (unlessTis a value type, in which case it creates the default value of that type):