I'm working on a simple function that has an instance of a class (in a loop) and calls a method which requires a parameter of the generic type of that class.
unfortunately i can't find any way to get this to compile, even with access to the specific Class object which the generic type is supposed to represent (via leaving a "hint" of the type in the instance)
for(Info<?> i : data.values()) {
if (i.atDefault())
i.setValue(base.data.get(i.getName()));
}
the error i get is in setValue:
The method setValue(capture#46-of ?) in the type Info<capture#46-of ?> is not applicable for the arguments (Info<capture#48-of ?>)Java(67108979)
the definition of setValue() is like public void setValue(T newValue). So i understand that the compiler has no way to know that the parameter base.data.get(i.getName()) is of type T. However, even if i use a Class.cast() to the type I expect T to be, the compiler does not accept this even though it should always work.
Casting the parameter to Object doesn't work because setValue() expects T, not Object. Casting to T doesn't work because it doesn't know what that is in the scope. The ? unknown type syntax isn't applicable here. As far as I can tell, there is no way for the compiler to know that any cast that I make is actually of type T, even if it happens to be, or I know that it is.
I'm stumped on how to actually get this to work, all I want is to be able to call a function on any Info object and pass it parameters of the type of which it is.