Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Either, Try, and Validation in Scala

I am confused with Either, Try, and Validation of scalaz. None of them seems to do what I need. What I need is a simple monad Result[E, R] where E is an error type and R is an result type.

  • Either is not suitable because it is not a monad (but the projections are) and unbiased.
  • Try is not suitable since its error type is Throwable and it is not exactly a monad
  • Validation is not a monad at all.

What would you suggest ? Should I write this Result type by myself ?

like image 323
Michael Avatar asked Mar 13 '14 12:03

Michael


1 Answers

If you're willing to use Scalaz (and it sounds like you are), \/ (usually pronounced "disjunction") is exactly what you're looking for—a monadic, right-biased version of Either.

It also includes lots of other nice stuff you don't get with the right projection of Either in the standard library (1.right syntax, combinators like +++, converters from Validation, etc.).

like image 188
Travis Brown Avatar answered Nov 15 '22 04:11

Travis Brown