#method call with generic type parameter

18 messages · Page 1 of 1 (latest)

fickle kelp
#

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.

bleak juniperBOT
#

This post has been reserved for your question.

Hey @fickle kelp! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

fiery knoll
#

Dude, you used <?>

#

Nothing will be accepted

#

<?> is not <T>, what would a cast achieve?

fickle kelp
#

is there an alternate way to accomplish what I'm trying to do?

The list data.values() has Info objects with various different generic types

#

so I can't do for(Info<AnySpecificType> i : data.values())

fickle kelp
# fiery knoll <?> is not <T>, what would a cast achieve?

i can accept that Info<?> may not be the way to go but I don't know how to achieve my end goal, which is to have a list of Info with various types, and call a Info.setValue() function with a parameter of the type that they have

fiery knoll
#

You have contradictory goals

#

You don't have a type consistency in place, and you're trying to use a system that guarantees type consistency

#

If you really want to do something like that, then you have to ignore generics on that part

fickle kelp
#

i could abandon generics but I would have to write a lot of duplicated code for every type that i wanted Info to represent

#

for example i'd have to write a setValue() in a subclass for each of them when it is all the same code over and over

fiery knoll
#

I said ignore generics on that part. Not necessarily remove them entirely

#

Anyway, you could flat-out remove the <> part altogether so that generics aren't checked, which is your goal as you don't have type safety on that part.

Or you could "lie" and say it's Info<Object> in the loop so that it will accept anything inside the loop. You'll need to tell the compiler to just accept the lie by casting

fickle kelp
#

i think i figured out something that works - i changed setValue() to accept type Object, then check instanceOf using the type hints i have, and throw ClassCastException if it has been used incorrectly.

i'll keep what you said in mind though, because those also seem like they would work

#

i appreciate the help!