简体   繁体   English

Scala中的任何一种,尝试和验证

[英]Either, Try, and Validation in Scala

I am confused with Either , Try , and Validation of scalaz . 我对scalaz EitherTryValidation感到困惑。 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. 我需要的是一个简单的monad Result[E, R] ,其中E是错误类型, R是结果类型。

  • Either is not suitable because it is not a monad (but the projections are) and unbiased . Either ,因为它不是一个单子(但突起是)和无偏见是不适合的。
  • Try is not suitable since its error type is Throwable and it is not exactly a monad Try不合适,因为它的错误类型是Throwable并且它不完全是monad
  • Validation is not a monad at all. Validation根本不是monad。

What would you suggest ? 你会建议什么? Should I write this Result type by myself ? 我应该自己写这个Result类型吗?

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 . 如果你愿意使用Scalaz(这听起来像你是), \\/ (通常发音“脱节”)是你在寻找什么,一个单子,右偏版本的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.). 它还包括很多其他很好的东西,你没有在标准库中正确投影Either1.right语法,像+++这样的组合器,来自Validation转换器等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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