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 isThrowable
and it is not exactly a monadValidation
is not a monad at all.
What would you suggest ? Should I write this Result
type by myself ?
Try
not monads ? What monad laws does it break ? – GarnisheeTry
breaks the unit law in favor of safety, only in the case where an exception is thrown. If you havef: A => Try[B]
, thenTry(a).flatMap(f)
has identical behavior tof(a)
according to the unit law. But iff(a)
throws an exception,Try
'sflatMap
will catch it. Hence, in case of exceptionTry
is non-monadic because monadicity is irreconcilable with safety from exceptions. Since the point ofTry
is exception-safety, it favors safety. (If you restrict yourself to inputs that do not throw exceptions,Try
is a monad.) – Indisputable