2014-06-05 21 views
6

假設我有以下代碼:Scala的要麼元組做爲右

val either: Either[String, (Int, Int)] = Right((1,2)) 
for { 
    (a, b) <- either.right 
} yield a + b 

當我評價它在REPL我得到

:13: error: constructor cannot be instantiated to expected type; found : (T1, T2) required: scala.util.Either[Nothing,(Double, Double)] (a, b) <- a.right ^:14: error: not found: value a } yield a + b ^

爲什麼我有這樣的錯誤?我無法從任何一個模式中對元組進行模式匹配?

回答

5

問題似乎是一個斯卡拉bug https://issues.scala-lang.org/browse/SI-7222。將for comprehension轉換爲flatMap/map符號似乎可行。

val either: Either[String, (Int, Int)] = Right((1, 2)) 
either.right.map { 
    case (a, b) => 
    a + b 
} 

either: Either[String,(Int, Int)] = Right((1,2)) 
res0: Serializable with Product with scala.util.Either[String,Int] = Right(3) 
+0

我正在使用scala 2.10.3。另外爲什麼它有這樣一個奇怪的類型:'用scala.util.Etil產生的可序列化的[String,Int]'?爲什麼不只是'scala.util.E [String,Int]' – maks