Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use a wild card capture helper method?

Referring to : Wildcard Capture Helper Methods

It says to create a helper method to capture the wild card.

public void foo(List<?> i) {
    fooHelper(i);
}        
private <T> void fooHelper(List<T> l) {
    l.set(0, l.get(0));
}

Just using this function below alone doesn't produce any compilation errors, and seems to work the same way. What I don't understand is: why wouldn't you just use this and avoid using a helper?

public <T> void foo(List<T> l) {
    l.set(0, l.get(0));
}

I thought that this question would really boil down to: what's the difference between wildcard and generics? So, I went to this: difference between wildcard and generics. It says to use type parameters:

1) If you want to enforce some relationship on the different types of method arguments, you can't do that with wildcards, you have to use type parameters.

But, isn't that exactly what the wildcard with helper function is actually doing? Is it not enforcing a relationship on different types of method arguments with its setting and getting of unknown values?

My question is: If you have to define something that requires a relationship on different types of method args, then why use wildcards in the first place and then use a helper function for it?

It seems like a hacky way to incorporate wildcards.

like image 355
Brandon Ling Avatar asked Jun 10 '15 18:06

Brandon Ling


People also ask

What is wildcard capture?

In some cases, the compiler infers the type of a wildcard. For example, a list may be defined as List<?> but, when evaluating an expression, the compiler infers a particular type from the code. This scenario is known as wildcard capture.

What is the use of wildcard in Java?

In the Java programming language, the wildcard ? is a special kind of type argument that controls the type safety of the use of generic (parameterized) types. It can be used in variable declarations and instantiations as well as in method definitions, but not in the definition of a generic type.

What is capture in Java?

wildcard capture is the process of binding the value of a wildcard type to a new type variable.

Which among the following is valid option for wildcards?

Which of these is wildcard symbol? a) ? b) ! Explanation: In generic code, the question mark (?), called the wildcard, represents an unknown type.


2 Answers

In this particular case it's because the List.set(int, E) method requires the type to be the same as the type in the list.

If you don't have the helper method, the compiler doesn't know if ? is the same for List<?> and the return from get(int) so you get a compiler error:

The method set(int, capture#1-of ?) in the type List<capture#1-of ?> is not applicable for the arguments (int, capture#2-of ?)

With the helper method, you are telling the compiler, the type is the same, I just don't know what the type is.

So why have the non-helper method?

Generics weren't introduced until Java 5 so there is a lot of code out there that predates generics. A pre-Java 5 List is now a List<?> so if you were trying to compile old code in a generic aware compiler, you would have to add these helper methods if you couldn't change the method signatures.

like image 119
dkatzel Avatar answered Sep 28 '22 05:09

dkatzel


I agree: Delete the helper method and type the public API. There's no reason not to, and every reason to.

Just to summarise the need for the helper with the wildcard version: Although it's obvious to us as humans, the compiler doesn't know that the unknown type returned from l.get(0) is the same unknown type of the list itself. ie it doesn't factor in that the parameter of the set() call comes from the same list object as the target, so it must be a safe operation. It only notices that the type returned from get() is unknown and the type of the target list is unknown, and two unknowns are not guaranteed to be the same type.

like image 23
Bohemian Avatar answered Sep 28 '22 03:09

Bohemian