[英]scala.util.Try: How to get Throwable value? Pattern matching?
The following REPL snippets presume: 以下REPL片段假定:
import scala.util.{Try, Success, Failure}
Why do these two statements not pass compilation? 为什么这两个语句没有通过编译? I get "constructor cannot be instantiated to expected type": 我得到“构造函数无法实例化为期望的类型”:
Failure(new Exception("a")) match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }
Success(123) match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }
I can get the success value out of a Try
with get
or toOption
. 我可以通过Try
with get
或toOption
获得成功价值。 Is there a corresponding way to get the failing Throwable
value or Option[Throwable]
? 是否有相应的方法来获取失败的Throwable
值或Option[Throwable]
?
EDIT: Casting from Failure/Success to Try works 编辑:从失败/成功投射尝试工作
Failure(new Exception("a")).asInstanceOf[Try[Int]] match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }
Success(123).asInstanceOf[Try[Int]] match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }
Consider this: 考虑一下:
Try(1) match {
case Success(i) => i
case Failure(t) => 0 // t is the `Throwable`
}
This works because Success
and Failure
are sub classes of the abstract class Try
. 这是有效的,因为Success
和Failure
是抽象类Try
子类。 However, the following code fails to compile, because you're no longer matching on a generic Try
, and instead a Failure
which can never be an instance of Success
. 但是,以下代码无法编译,因为您不再匹配一般的Try
,而是一个永远不会成为Success
的实例的Failure
。
Failure(new Exception("a")) match {
case Success(i) => "a" // You can see it compiles if you remove this line.
case Failure(e) => "b"
case _ => "c"
}
This is like trying to match an Integer
to a String
, it doesn't really make sense. 这就像尝试将Integer
与String
匹配一样,它实际上没有意义。
If you want to get the Throwable
via pattern matching, see the first snippet of code. 如果您想通过模式匹配获得Throwable
,请参阅第一段代码。
Another way you could extract the Throwable
would be to use the failed
method on your Try
, which will wrap the Throwable
from a failure within Success
. 另一种可以提取Throwable
的方法是在Try
上使用failed
方法,它将Throwable
从Success
的失败中包装起来。
scala> val t: Throwable = Try(throw new Exception).failed.get
t: Throwable = java.lang.Exception
Calling this on a Success
, however, will throw another exception. 然而,在Success
调用它会引发另一个异常。
The first snippet doesn't compile because Success
isn't a subtype of Failure
. 第一个代码段无法编译,因为Success
不是Failure
的子类型。 The compiler thinks you're being silly, since the Success(i)
case will never match. 编译器认为你很傻,因为Success(i)
情况永远不会匹配。
To take a simpler example, this also doesn't compile. 举一个更简单的例子,这也不能编译。
Failure(new Exception()) match { case Success(_) => }
Neither does this, for the same reason. 出于同样的原因,这也不是这样。
42 match { case Success(_) => }
What you've written does nearly work, but the value you're matching has to have a more general type of Try[_]
(which it would have anyway, if the pattern match were actually being used in some useful context). 你所编写的内容几乎可以正常工作,但是你匹配的值必须有一个更通用的Try[_]
类型(如果模式匹配实际上在某些有用的上下文中使用的话,它将具有它。)
(Failure(new Exception("a")): Try[_]) match {
case Success(i) => "a"; case Failure(e) => "b"; case _ => "c"; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.