Are there generics that can act like varargs in Java?
E.g.
class Test<...T> {
private final T[0] val = testarg;
public Test(T[0] testarg) {
val = testarg;
}
}
Are there generics that can act like varargs in Java?
E.g.
class Test<...T> {
private final T[0] val = testarg;
public Test(T[0] testarg) {
val = testarg;
}
}
For further actions, you may consider blocking this person and/or reporting abuse
ivan.gavlik -
happyer -
Shai Almog -
Thodoris Kouleris -
Top comments (12)
This thing is called "variadic template" in C++ and, unfortunately, is not available in Java. Such a functionality requires implementation of generics as templates while in Java they're implemented using type erasure.
What's a template?
Give me a guide to this C++ godsend.
Well, the real guide would be a thick book like C++ Templates: The Complete Guide. But in brief, this is a technique, which allows the compiler to generate classes at compile time using the description of a generic class as a template. With this approach the compiler creates a new class for every type argument passed to template. For example, if your Java code uses
List<String>
andList<Integer>
, both of these classes are actually the same instance of classList
with type of contained element erased at compile time. In C++ same types will result in two different classes, where each class has its own set of methods and possibly different internal layout of the class. There are much more details and both approaches are not so simple, but overall high level picture looks as described above. By the way, if you're really interested in gaining more knowledge about C++ templates, the book mentioned above is one of the best among those dedicated to the topic.Do you mean something like the following?
Yes. But, just like varargs, I want the user to enter as many as needed.
The user can’t input actual types, so I don’t see how that could be done.
Well, aren't "Types" just classes? Even primitives TYPES are classes, too.
When you use a generic, doesn't it just send the reference of the class to the variable?
?
Is this true, or no?
I'm talking about the actual user input, not the developer.
With regard to your example, in Java that variable will be an object of type Test, which at compile time will have verified to use the type String in all cases where T is used in the general definition of the class. So no, no reference of the class is sent to the variable.
What if I use
String.class
?String.class is a object representing the String type. But that's not what is needed here.
Think of it like this: if you have a generic box, a banana box and an apple box, then you can't transform a generic box into a banana box by throwing a banana at it. The banana needs to conceptually be in the picture when the box its design is created.
Okay.
Thanks.
Cheers.
Oh. Is there any way I can include more than one, but a never finite amount of generics (safely)?
I really want to use it for something like this:
Having something like that'd be cool.
Thanks for you answer.
Cheers.