意外的vavr的Either在reactor中的返回类型

huangapple go评论223阅读模式
英文:

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&lt;String, Integer&gt; testEither(int s) {
    if (s == 0)
        return Either.left(&quot;Wrong&quot;);
    return Either.right(s);
}

public Mono&lt;Either&lt;String, Integer&gt;&gt; testReactorEither(int s) {
    return Mono.just(s).filter(x -&gt; x == 0).map(Either::right)
            .switchIfEmpty(Mono.just(Either.left(&quot;ERROR&quot;)));
}

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&lt;io.vavr.control.Either&lt;java.lang.Object,java.lang.Integer&gt;&gt; is incompatible with required return of reactor.core.publisher.Mono&lt;io.vavr.control.Either&lt;java.lang.String,java.lang.Integer&gt;&gt;.

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&lt;?, Integer&gt;. 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/

https://openjdk.java.net/jeps/101

答案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&lt;Either&lt;String, Integer&gt;&gt; testReactorEither(int s) {
    return Mono.just(s)
            .filter(x -&gt; x == 0)
            .&lt;Either&lt;String, Integer&gt;&gt;map(Either::right)
            .switchIfEmpty(Mono.just(Either.left(&quot;ERROR&quot;)));
}

huangapple
  • 本文由 发表于 2020年8月20日 09:21:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63497038.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定