I have the following method declaration:
public <T extends Serializable,
Method extends BotApiMethod<T>,
Callback extends SentCallback<T>>
void executeAsync(Method method, Callback callback)
When I use it in the following way:
executeAsync(editMessage, new SentCallback() {
it works fine but shows the type safety warnings.
But when I provide the generic type to the SentCallback
, like:
executeAsync(editMessage, new SentCallback<Message>()
it show the following compilation error:
The method executeAsync(Method, Callback) in the type AbsSender is not applicable for the arguments (EditMessageReplyMarkup, new SentCallback(){})
Why I can't use it with new SentCallback<Message>
?
First, let us take a look at the inferred type of T
: given the types of your parameters (BotApiMethod<Serializable>
and SentCallback<Message>
), T
should be either inferred to Serializable
or to Message
. But, at the same time, the current implementation forces that both parameters have the exact same T
. This is the reason the presented attempt does not work. Keep in mind that in Java, generics are invariant.
If one cannot change the given definition of executeAsync(...)
, the problem is not solvable with the parameters provided.
If parameter T
is essential and the design allows for different generic parameters of Method
and Callback
, which only need to have a common supertype T
, the definition of executeAsync(...)
could be changed to:
public <T extends Serializable,
Method extends BotApiMethod<? extends T>,
Callback extends SentCallback<? extends T>>
void executeAsync(Method method, Callback callback)
If parameter T
is not essential it could be dropped completely, leading to the following definition of executeAsync(...)
:
public <Method extends BotApiMethod<? extends Serializable>,
Callback extends SentCallback<? extends Serializable>>
void executeAsync(Method method, Callback callback)