Say I have a utils function:
public static <T extends Number> double myFunc(List<T> a1, List<T> a2) {...}
Next, I have a class:
class Config<T extends Number>{
public List<T> buckets;
public List<T> groups;
}
Finally, I want to write a function, like so:
double execute(Config<? extends Number> config){
//Some stuff
myFunc(config.buckets, config.groups)
}
However, this gives me a compile time error: "The method myFunc(List< T>, List< T>) is not applicable for the arguments (List< capture#1-of ? extends Number>, List< capture#2-of ? extends Number>)".
I have a guess as to why the compiler doesn't like this.. Basically, myFunc
requires both argument lists to have the same type parameter T extends Number
, but I'm supplying ? extends Number
for both, so it has no way to ensure that both will be the same type parameter? (I've encountered this error, so I'm guessing that's responsible here as well..) But I'm using the same Config<? extends Number> config
object's buckets
and groups
fields, which if you see the class definition, have the same type T
as defined in the class level type parameter. So aren't they guaranteed to have the same type parameter?
Is the issue somewhere else?
Thanks in advance for making it all the way to the end.