Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala中的奇异模式匹配_Scala_Pattern Matching - Fatal编程技术网

Scala中的奇异模式匹配

Scala中的奇异模式匹配,scala,pattern-matching,Scala,Pattern Matching,我遇到了以下代码: val f = Future { throw new InterruptedException } f.failed foreach { case t => log(s"error - $t") } 代码使用模式匹配 在模式匹配中,将值与模式进行比较。但我没有看到任何模式。t看起来更像值。那么它是如何工作的呢?这一节明确指出: 变量模式x是以小写字母开头的简单标识符。它匹配任何值,并将变量名绑定到该值 变量t以小写字母开头 变量模式是“简单模式”,而简单模式又是一般模

我遇到了以下代码:

val f = Future { throw new InterruptedException }
f.failed foreach { case t => log(s"error - $t") }
代码使用模式匹配

在模式匹配中,将值与模式进行比较。但我没有看到任何模式。t看起来更像值。那么它是如何工作的呢?

这一节明确指出:

变量模式x是以小写字母开头的简单标识符。它匹配任何值,并将变量名绑定到该值

变量
t
以小写字母开头

变量模式是“简单模式”,而简单模式又是一般模式的特例。

这可以帮助您吗

/** Asynchronously processes the value in the future once the value becomes available.
   *
   *  WARNING: Will not be called if this future is never completed or if it is completed with a failure.
   *
   *  $swallowsExceptions
   *
   * @tparam U     only used to accept any return type of the given callback function
   * @param f      the function which will be executed if this `Future` completes with a result,
   *               the return value of `f` will be discarded.
   * @group Callbacks
   */
  def foreach[U](f: T => U)(implicit executor: ExecutionContext): Unit = onComplete { _ foreach f }
这是关于Future trait的foreach文档,内部foreach是从scala.util.Try.foreach调用的 还有他,德克拉里昂

 /**
   * Applies the given function `f` if this is a `Success`, otherwise returns `Unit` if this is a `Failure`.
   *
   * ''Note:'' If `f` throws, then this method may throw an exception.
   */
  def foreach[U](f: T => U): Unit
这是最重要的。
*警告:如果此未来从未完成或失败而完成,则不会调用此未来。

抱歉,在回答之前我不理解您的问题。。。。但是不,如果你返回一个新的异常,它不会抛出一个异常,它只会在你抛出它时发生,例如,在你创建的时候。