英文:
Unexpected return type of vavr's Either in reactor
问题
以下是您要翻译的内容:
有两种使用vavr的Either的简单方法。
public Either<String, Integer> testEither(int s) {
if (s == 0)
return Either.left("Wrong");
return Either.right(s);
}
public Mono<Either<String, Integer>> testReactorEither(int s) {
return Mono.just(s).filter(x -> x == 0).map(Either::right)
.switchIfEmpty(Mono.just(Either.left("ERROR")));
}
testEither方法正常工作,但与此同时,testReactorEither方法引发了"不兼容类型"的编译错误,错误消息说提供的返回类型"reactor.core.publisher.Mono<io.vavr.control.Either<java.lang.Object,java.lang.Integer>>"与所需返回类型"reactor.core.publisher.Mono<io.vavr.control.Either<java.lang.String,java.lang.Integer>>"不兼容。
我担心问题只是因为"map(Either::right)"方法只定义了"Integer"的"Right"类型,但没有定义"Left"类型,然后方法的返回类型是"Either<?, Integer>"。那么问题是,在这种情况下,我如何获得预期的返回类型呢?
[已更新]
正如Hinse在他的评论中提到的,这个问题与Java类型推断的限制有关,我找到的一些与该问题相关的链接如下:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=511252
https://e.printstacktrace.blog/java-type-inference-generic-methods-chain-call/
https://openjdk.java.net/jeps/101
英文:
There are two simple methods using vavr's Either.
public Either<String, Integer> testEither(int s) {
if (s == 0)
return Either.left("Wrong");
return Either.right(s);
}
public Mono<Either<String, Integer>> testReactorEither(int s) {
return Mono.just(s).filter(x -> x == 0).map(Either::right)
.switchIfEmpty(Mono.just(Either.left("ERROR")));
}
The testEither
works normally, but meanwhile, the testReactorEither
raises a compile error of "incompatible types" which says the provided return type of reactor.core.publisher.Mono<io.vavr.control.Either<java.lang.Object,java.lang.Integer>>
is incompatible with required return of reactor.core.publisher.Mono<io.vavr.control.Either<java.lang.String,java.lang.Integer>>
.
I'm afraid the problem is just because the method of map(Either::right)
just defines the Right
type of Integer
but not do the Left
type, and then the return type of the method is Either<?, Integer>
. Then the question is how I can get the expected return type in this case?
[UPDATED]
As Hinse mentioned in his comment, the issue is related to the limitation of Java type inference, and some links I found for the problem is listed as follows:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=511252
https://e.printstacktrace.blog/java-type-inference-generic-methods-chain-call/
答案1
得分: 1
第二个示例中,在应用map(Either::right)
后,左侧似乎丢失了类型信息。
在map方法调用中添加一些类型提示应该可以解决问题。因此,testReactorEither将如下所示:
public Mono<Either<String, Integer>> testReactorEither(int s) {
return Mono.just(s)
.filter(x -> x == 0)
.<Either<String, Integer>>map(Either::right)
.switchIfEmpty(Mono.just(Either.left("ERROR")));
}
英文:
The second example seems to 'lose' the type information for the left side when the map(Either::right)
is applied.
Adding some type 'hints' to the map method call should do the trick. So the testReactorEither will look like this:
public Mono<Either<String, Integer>> testReactorEither(int s) {
return Mono.just(s)
.filter(x -> x == 0)
.<Either<String, Integer>>map(Either::right)
.switchIfEmpty(Mono.just(Either.left("ERROR")));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论