为什么与元组类型的提取器一起使用的 for-comprehension 会导致对“filter”的编译警告?

Why does a for-comprehension used with an extractor of type tuple result in a compile warning on `filter`?

给定以下代码片段:

import scala.util.Try

def foo(x:Int) : (Int, String) = {
  (x+1, x.toString)
}

def main(args: Array[String]) : Unit = {
  val r1: Try[(Int, String)] = for {
    v <- Try { foo(3) }
  } yield v

  val r2: Try[(Int, String)] = for {
    (i, s)  <- Try { foo(3) } // compile warning refers to this line
  } yield (i, s)
}

1.为什么编译上述代码会抛出以下警告?

`withFilter' method does not yet exist on scala.util.Try[(Int, String)], using `filter' method instead
[warn]       (i, s)  <- Try { foo(3) }
[warn]                      ^
[warn] one warning found

2。为什么在提取到元组时完全使用 withFilter

更新

独立于警告消息,我非常想知道是否使用了 withFilter? (见问题 2)

似乎 Try.withFilter 是在 2.11 中才添加的(参见 Try 2.10.6 and Try 2.11.0

for comprehension中使用

withFilter代替filter因为比较懒,更全面的对比可以看this question.

你的第二个理解类似于:

Try(foo(3)).withFilter {
  case (i, s) => true
  case _      => false
} map(identity)

因为在 Scala 2.10.5 中 Try.withFilter 不存在,它回退到使用 filter(创建一个新的 Try)。


编辑:为什么你需要 withFilter 在你的情况下并不那么明显,因为你实际上并没有使用 (i, s) 模式匹配进行过滤.

如果你在下面写了 for comprehension,当你在 for comprehension 的左侧添加模式匹配时,你(可以)过滤会更清楚。

for {
  (i, "3") <- Try(foo(3))
} yield i

类似于:

Try(foo(3)) withFilter {
  case (i, "3") => true
  case _        => false
} map { case (i, "3") => i }

如您所见,withFilter 不仅在添加 if 保护时使用,而且在模式匹配时也使用。