javaeithervavr

How to chain vavr's Either nicely?


I have a function returning an Either<MyError, String> (function2) , which result depends on another function returning another Either<MyError, SomethingElse> (function1)
Both functions rely on a Try block that could fail, and I want to compose those two first function to create a "handle" which will be the main function of my class.

There are basically 3 scenarios possible :

Here is my current code :

private Either<MyError, Path> getPath(Arg arg) { // function 1
  return Try.of(() -> //some code that can fail)
              .toEither().mapLeft(e -> new MyError("Error message for function1", e));
}

private Either<MyError, String> getContent(Path path) { // function 2
  return Try.of(() -> //some code that can fail)
              .toEither().mapLeft(e -> new MyError("Error message for function2", e));
}

public Either<MyError, String> handle(Arg arg) {
  return Either.right(arg)
                .map(this::getPath)
                .map(this::getContent);
}

Everything works except the Handle function, I think that my problem might be related to the use of Either::map function, that might not be the thing for my case.

Any thought about this ?

Also, sorry if the answer seems obvious, i am quite new to functionnal programming and vavr.


Solution

  • The method that could help to make this work would be flatMap.

    So if you use flatMap instead of map, the handle method will become something like:

    public Either<MyError, String> handle(Arg arg) {
        return Either.<MyError, Arg>right(arg)
            .flatMap(this::getPath)
            .flatMap(this::getContent);
    }
    

    The scenarios you mentioned are all covered with this flatMap method.

    See the Either.flatMap documentation for the official docs about it.