U
U
User7002021-11-02 19:35:38
Java
User700, 2021-11-02 19:35:38

Is it possible to use generic like C++ templates?

Is it possible to do something like this:

private LongPollServer initServer (Resp1 r)  .....
private LongPollServer initServer (Resp2 r)  .....
private LongPollServer initServer (Resp3 r)  .....

protected <R> void run (ApiRequest<R> req)
{
    .....
    LongPollServer lps = initServer((R)req.exec());
    .....
}

It is known that run will be called from types that inherit from ApiRequestBuilder[ApiReqest[Resp1],Resp1], which inherits from ApiReqest[Resp1]. And similarly with 2.3. Read square brackets as angle brackets.
exec will return Resp1,2,3 respectively.
Perhaps you need to use generic with questions. familiar...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
User700, 2021-11-02
@User700

In the end I wrote this:

private LongPollServer initServer (Object lpServerResponse) {
    if (lpServerResponse instanceof GetLongPollServerResponse)
          return new LongPollServer()
                .setKey(((GetLongPollServerResponse)lpServerResponse).getKey())
                .setTs(((GetLongPollServerResponse)lpServerResponse).getTs())
                .setServer(((GetLongPollServerResponse)lpServerResponse).getServer());
    else if (lpServerResponse instanceof com.vk.api.sdk.objects.messages.responses.GetLongPollServerResponse)
      return new LongPollServer()
                .setKey(((com.vk.api.sdk.objects.messages.responses.GetLongPollServerResponse)lpServerResponse).getKey())
                .setTs(((com.vk.api.sdk.objects.messages.responses.GetLongPollServerResponse)lpServerResponse).getTs().toString())
                .setServer(((com.vk.api.sdk.objects.messages.responses.GetLongPollServerResponse)lpServerResponse).getServer());
    else
      return new LongPollServer()
                .setKey(((com.vk.api.sdk.objects.callback.longpoll.responses.GetLongPollServerResponse)lpServerResponse).getKey())
                .setTs(((com.vk.api.sdk.objects.callback.longpoll.responses.GetLongPollServerResponse)lpServerResponse).getTs())
                .setServer(((com.vk.api.sdk.objects.callback.longpoll.responses.GetLongPollServerResponse)lpServerResponse).getServer());
    }

But it doesn't work for other reasons :(

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question