Sample available here: https://github.com/codependent/hexapp-java/blob/vavr/src/main/java/com/codependent/hexapp/application/port/in/impl/CreateDepartmentUseCaseImpl.java
I have chained Either operations that require the usage of Either.flatmap:
@Override
public Either<? extends ApplicationError, Department> createDepartment(CreateDepartmentCommand command) {
Either<? extends ApplicationError, Department> department = Department.create(command.id(), command.name());
Either<? extends ApplicationError, Department> createdDepartment = department.flatMap((Department dep) -> {
Optional<Department> existingDepartment = getDepartmentDrivenPort.getByName(command.name());
if (existingDepartment.isPresent()) {
Either<? extends ApplicationError, Department> left = Either.left(new DepartmentExistsError());
return left;
} else {
Either<? extends ApplicationError, Department> right = createDepartmentDrivenPort.create(dep);
return right;
}
});
return createdDepartment;
}
This code shows this compilation error:
java: method flatMap in interface io.vavr.control.Either<L,R> cannot be applied to given types;
required: java.util.function.Function<? super com.codependent.hexapp.application.domain.Department,? extends io.vavr.control.Either<capture#1 of ? extends com.codependent.hexapp.application.domain.error.ApplicationError,? extends U>>
found: (Departmen[...]; } }
reason: cannot infer type-variable(s) U
(argument mismatch; bad return type in lambda expression
io.vavr.control.Either<capture#2 of ? extends com.codependent.hexapp.application.domain.error.ApplicationError,com.codependent.hexapp.application.domain.Department> cannot be converted to io.vavr.control.Either<capture#1 of ? extends com.codependent.hexapp.application.domain.error.ApplicationError,? extends U>)
I can't see whats the problem here since U is Department
...
I also tried autogenerating the Function using IntelliJ's assistant but this code doesn't compile either:
department.flatMap(new Function<Department, Either<? extends ApplicationError, ?>>() {
@Override
public Either<? extends ApplicationError, ?> apply(Department department) {
return null;
}
});
java: method flatMap in interface io.vavr.control.Either<L,R> cannot be applied to given types;
required: java.util.function.Function<? super com.codependent.hexapp.application.domain.Department,? extends io.vavr.control.Either<capture#1 of ? extends com.codependent.hexapp.application.domain.error.ApplicationError,? extends U>>
found: <anonymous java.util.function.Function<com.codependent.hexapp.application.domain.Department,io.vavr.control.Either<? extends com.codependent.hexapp.application.domain.error.ApplicationError,?>>>
reason: cannot infer type-variable(s) U
(argument mis