How to assign return value to an object from a generic function with wildcard return type?

Viewed 73

This could be very basic question but I am not able to figure out this, I am trying to call a function from some library. The signature of this function is as follows.

<V> Channel<?,V> create(@Nonnull ChannelSetting setting);

I am calling this method by simply passing an object of ChannelSetting in param, And I want to assign its returned result in Channel<String, SomeClassType> object. If I am doing it as follows,

Channel<String, SomeClassType> result = creatorObj.create(setting);

I a getting compilation error Type Mismatch.

Type mismatch: Cannot convert from Channel<capture#1-of ?, Object> to Channel<String,SomeClassType>

I am not understanding how to assign return type, I can not leave <?> because I need it for specific type only.
Thanks,

1 Answers

The method signature is telling you that create() returns a Channel where the first type variable is something you don't know. You can't safely assign it to, for example, Channel<String, SomeClassType> because it could in fact be a Channel<Integer, SomeClassType>; based on what you've shown of the API, there's no way to tell.

Related