为什么 Vavr Either 不识别 map() 函数的参数?

Why isn't Vavr Either is recognizing the parameter to map() function?

我正在用神奇的 vavr (0.9.2) 库弄脏我的手。

这是一个代码片段,旨在收集 Either:

    Either<Tuple2<Enum<ReportByTeamExecutionErrors>,String>, List<MayurDAO>> payloadPreparationResult =
    new ReportByTeamCriteriaSchemaChecker("./json/schema/REQ_ReportByTeam_Schema.json")
    .validateSchema(payLoad) // payload is JSON and is a paramter to this holding function
.map(JsonPOJOBidirectionalConverter::pojofyCriteriaAllFieldsTeam)
.map((e) -> {
        CriteriaTeamAllFieldsValidator validator =  new CriteriaTeamAllFieldsValidator(e.right().get());
        return(validator.validate());
 })
 .map((e) -> retrieveRecordsAsPerCriteria(e.right().get())) // compiler doesn't approve of this line!!
 ;

retrieveRecordsAsPerCriteria 方法是这样定义的:

private Either<Tuple2<Enum<ReportByTeamExecutionErrors>,String>, List<MayurDAO>>
    retrieveRecordsAsPerCriteria(CriteriaAllFieldsTeam criteriaAllFieldsTeam) {

        Optional<List<MayurDAO>> retrievedRecords = new MayurModel().retrieveRecords(criteriaAllFieldsTeam);

        return( (retrievedRecords.isPresent())
                ? (Either.right(retrievedRecords.get()))
                : Either.left(
                        Tuple.of(
                             ReportByTeamExecutionErrors.NO_RECORDS_FOUND_TO_MATCH_CRITERIA,
                             "No records have been found, which match the criteria passed"
                        )
                       )
            );
    }

编译器报错:

./com/myApp/application/ReportByTeamResource.java:58: error: incompatible types: cannot infer type-variable(s) L,R .map((e) -> retrieveRecordsAsPerCriteria(e.right().get())); ^ (actual and formal argument lists differ in length) where L,R are type-variables: L extends Object declared in method right(R) R extends Object declared in method right(R) 1 error

列表来自java.util.List。

我无计可施,无法理解问题的根源。类型应该很容易推断出来,或者我认为。

IntelliJ 似乎可以接受直到这条令人反感的行的转换!然而,在那一行中,类型参数 U 是不可破译的。 出于某种原因,有人可以将我推到我看不到的路径吗?

您可能想使用 flatMap 而不是 mapflatMap 的优点是您不会得到嵌套的 either,然后您需要以某种方式 unwrap (就像您目前正在使用 .right().get()您当前的代码)。

所以代码看起来像这样:

Either<Tuple2<Enum<ReportByTeamExecutionErrors>,String>, List<MayurDAO>> payloadPreparationResult =
new ReportByTeamCriteriaSchemaChecker("./json/schema/REQ_ReportByTeam_Schema.json")
    .validateSchema(payLoad)
    .flatMap(JsonPOJOBidirectionalConverter::pojofyCriteriaAllFieldsTeam)
    .flatMap(e -> {
        CriteriaTeamAllFieldsValidator validator =  new CriteriaTeamAllFieldsValidator(e);
        return (validator.validate());
    })
    .flatMap(this::retrieveRecordsAsPerCriteria)