给定以下代码片段:
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
withFilter
2.提取到元组时为什么要使用?
更新
- Scala 2.10.5 出现警告
- Scala 2.11.7不会出现警告
独立于警告消息,我很想知道是否withFilter
使用了?(见问题 2)