更新时间:2022-03-28 21:54:43
Either
从未真正成为基于异常处理的结构.它旨在表示一种情况,其中一个函数实际上可能返回两种不同类型中的一种,但是人们开始约定,左类型应该是失败的情况,而右类型是成功的情况.如果要为某些通过/失败类型业务检查逻辑返回偏倚类型,则scalaz
中的Validation
效果很好.如果您有一个可以返回值或Throwable
的函数,那么Try
将是一个不错的选择. Either
应该用于您确实可能会获得两种可能的类型之一的情况,并且现在我正在使用Try
和Validation
(每种用于不同类型的情况),我再也不会使用Either
了.
Either
was never really meant to be an exception handling based structure. It was meant to represent a situation where a function really could possible return one of two distinct types, but people started the convention where the left type is a supposed to be a failed case and the right is success. If you want to return a biased type for some pass/fail type business checks logic, then Validation
from scalaz
works well. If you have a function that could return a value or a Throwable
, then Try
would be a good choice. Either
should be used for situations where you really might get one of two possible types, and now that I am using Try
and Validation
(each for different types of situations), I never use Either
any more.