简体   繁体   中英

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 ?

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.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM