0

I'm trying to extend this interface:

@FunctionalInterface
public interface HandlerFunction<T extends ServerResponse> {
    Mono<T> handle(ServerRequest request);
}

Like this:

@FunctionalInterface
public interface HandlerFn<R extends ServerRequest> extends HandlerFunction<ServerResponse> {
    default Mono<ServerResponse> handle(ServerRequest request) {
        return handleFn(request);
    }

    Mono<ServerResponse> handleFn(R request);
}

I'm getting this error (on handleFn(request)):

The method handleFn(R) in the type HandlerFn<R> is not applicable for the arguments (ServerRequest)

In this code snippet, why when calling handleFn(request) the type R is not inferred as ServerRequest ?

0

1 Answer 1

1

request is a ServerRequest, not an R. R could be ServerRequest, but it could be any other subclass of it.

R is declared on the class, not the method. The type of R is determined at the declaration of the reference to the HandlerFn, not at the call site of the handleFn method.

You could cast inside the default method:

return handleFn((R) request);

which is an unchecked cast (at that point; it would be checked elsewhere).

If you declared the method as a generic method, it would work in handle:

<RR extends ServerRequest> Mono<ServerResponse> handleFn(RR request);

(You wouldn't then need R on the interface. I just called it RR to emphasize that it is a separate type variable).

But, in fact, there is no point in such a type variable: this gives no advantage over declaring the method non-generically:

Mono<ServerResponse> handleFn(ServerRequest request);

which, of course, has no advantage over the handle method.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.