处理Scala中的单子的错误?尝试vs验证 [英] Error handling monads in Scala? Try vs Validation
问题描述
scalaz.Validation
据说比 Try
monad更强大,因为它可能会累积错误。
scalaz.Validation
is said to be more powerful than the Try
monad, because it can accumulate errors.
有没有可以选择尝试超过
scalaz.Validation
或 scalaz.\ / / / code>?
Are there any occasions where you might choose Try
over scalaz.Validation
or scalaz.\/
?
推荐答案
最重要的论据是支持尝试
是它在标准库中。在标准库中也使用 ,例如您使用 Future
的 onComplete $ c $注册的回调c>必须是
中的函数。尝试
。它可能会在今后的标准库中更广泛地使用。
The most significant argument in favor of Try
is that it's in the standard library. It's also used in the standard library—for example the callbacks you register with Future
's onComplete
must be functions from Try
. It may be used more extensively in the standard library in the future.
它在标准库中也意味着它会让更多人看起来很熟悉。您可能会倾向于在更多的第三方库中找到它。当然有时你可能不会被允许使用Scalaz(或任何其他的依赖),或者可能希望避免Scalaz的其他完美的理由。
The fact that it's in the standard library also means it'll look familiar to more people. You'll probably tend to find it in more of the third-party libraries you use. And of course sometimes you may not be allowed to use Scalaz (or any other dependencies) or may want to avoid Scalaz for other perfectly good reasons.
其他的东西:我可以不记得我最后一次写了一个 \ /
,它没有 Throwable
在它的左边(我这不是我经常做的事情)。 尝试
烘焙这个,所以你不必担心写一个别名或其他什么。
Other stuff: I can't remember the last time I wrote a \/
that didn't have Throwable
on its left side (I have—it's just not something I do often). Try
bakes this in, so you don't have to worry about writing an alias or whatever.
As <一个href =https://stackoverflow.com/users/406435/senia> senia 在上述评论中注意到,可以说有点不直观的偏好任一类型,但仍然使用左和右(如 \ /
是,而且)。为什么 \ /
绑定在右边?因为它是,所以这就是为什么。我个人没有发现令人反感的命名,但我可以理解为什么有些人可能会这样。 尝试
通过使构造函数名称明确指出其语义来避免此问题:成功
和失败
,而不是左
和右
或 -\ / code>和
\ / -
。
As senia notes in the comments above, there's arguably something a little unintuitive about biasing an either-like type but still using the language of "left" and "right" (as \/
is, and does). Why does \/
bind through the right side? Because it does, that's why. I personally don't find the naming that objectionable, but I can understand why some people might. Try
avoids the issue by having constructor names that clearly indicate their semantics: Success
and Failure
, not Left
and Right
or -\/
and \/-
.
现在我们已经完全肤浅主观原因使用尝试
,有些人可能会认为 \ /
和 -\ /
和 \ / -
是丑陋的。我通常不介意运算符重的代码,我发现斜杠和破折号的混乱真的令人不愉快的键入和阅读。
Now that we're getting to the completely superficial and subjective reasons to use Try
, some people may just think \/
and -\/
and \/-
are ugly. I generally don't mind operator-heavy code, and I find the jumble of slashes and dashes really unpleasant to type and read.
所以这些是有利于根据要求尝试
,但是我总结说,我从不使用它。我不是特别关心这个事实,它违反monad法律(虽然我可以理解人们为什么这样做),但我确实发现 \ /
和验证
更少的特别和更容易理解,我喜欢访问这两个(验证
当我想累积错误, \ /
当我需要单轨排序)在一个框架中。
So those are some arguments in favor of Try
, as requested, but I'll conclude by saying that I never use it, myself. I don't specifically care all that much about the fact that it violates the monad laws (although I can understand why people do), but I do find \/
and Validation
much less ad-hoc and easier to reason about, and I like having access to both (Validation
when I want to accumulate errors, \/
when I need monadic sequencing) in a single framework.
这篇关于处理Scala中的单子的错误?尝试vs验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!