5

I want to avoid:

Either<Error, Employee> processEmployee(Employee e)

and use:

Result<Employee> processEmployee(Employee e)

where left is fixed:

Result<T> extends Either<Error, T>

Is there an example of this?

When I try this nothing compiles for me and I'm forced to implement a concrete class of Result<T> which I want to avoid.

The reason I want to do this is to simplify the method signatures and the stream code that uses these methods.

jakstack
  • 2,143
  • 3
  • 20
  • 37
  • 6
    It sounds like you're looking for [type aliases](https://stackoverflow.com/questions/5604390/how-do-i-create-some-variable-type-alias-in-java), a feature which Java regrettably does not support. – Silvio Mayolo Jun 23 '21 at 20:43
  • It might work if you override all the methods that return `Either` and create some static method where you can provide an `Either` and return a `Result`. I ran into the same thing with Validation, but I'm a bit reluctant to override everything, it doesn't feel right on how we use vavr currently. – jvwilge Jul 12 '21 at 08:15
  • If your always going to use Either with your Error type maybe you could use Try instead and extend your type Error from Exception – Cristian Rodriguez Aug 13 '21 at 21:28

1 Answers1

0

What about using toTry method from Value interface? Your code could look like:

public Try<Employee> processEmployee(Employee e){
  //… blebleble

  var someEither<Error, Employee> = eitherResult();

  return someEither.toTry();
}

You have „simplified” type that contains everything that you need and contains toEither() method (if you need that).

Koziołek
  • 2,791
  • 1
  • 28
  • 48