Reputation: 223
I want to use the handle
operator, but the result of it is not the type I expect, it is always Object
Mono.just("lol").handle((string, sink) -> {
if(!string.equals("lol")) {
sink.error(new RuntimeException("not lol!"));
} else {
sink.next(2);
}
}).doOnNext(myInt -> { // expecting myInt to be an integer but is Object
System.out.println(myInt);
});
How can I get handle to recognize the type (similar to how map
or flatMap
recognizes the return type)?
Do I always have to use the cast
operator?
Upvotes: 6
Views: 3091
Reputation: 11561
Use generics.
Mono.<String>just("lol").<Integer>handle((string, sink) -> {
if(!string.equals("lol")) {
sink.error(new RuntimeException("not lol!"));
} else {
sink.next(2);
}
}).doOnNext(myInt -> {
System.out.println(myInt);
})
Upvotes: 11