Created
April 16, 2012 11:28
-
-
Save rirakkumya/2397904 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> for{(x,y) <- Right((3,4)).right} yield (x,y) | |
<console>:10: error: constructor cannot be instantiated to expected type; | |
found : (T1, T2) | |
required: Either[Nothing,(Int, Int)] | |
for{(x,y) <- Right((3,4)).right} yield (x,y) | |
scala> Right((3,4)).right map {case (x,y) => (x,y)} | |
res118: Product with Either[Nothing,(Int, Int)] with Serializable = Right((3,4)) |
Right((3,4)).right flatMap {case (x,y) => Right(x,y)}
なら通りますが、そういう事がやりたい訳ではないですよね…?
val Right((x,y)) = Right((3,4))
val Right(t) = Right((3,4))
val t = Right((3,4)).right.get
とかですか?
forでエラーになったので、flatmapだとどうなるか試してみただけのコードでした。紛らわしくてすみません。
この問題解決しました。
http://stackoverflow.com/questions/5831453/why-does-this-scala-for-expression-using-tuples-fail-to-compile
scalaの言語仕様85ページに、(http://www.scala-lang.org/docu/files/LangSpec2.8-ja_JP.pdf)
変換方式は次の通りです。 最初のステップでは、全ての生成子 p <- e は次で置き換えられます
p <- e.withFilter { case p => true; case _ => false }
と書いてあります。
scala> Right((2,3)).right.filter{case (x,y) => true;case _ => false}.get.right.map{case (x,y) => (x,y)}
res5: Product with Either[Nothing,(Int, Int)] with Serializable = Right((2,3))
filterが
Option[Either[Nothing,T]]
じゃなくて
Either.RightProjection[Nothing,T]
を返せばいいのに!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
エラーの出方がちょっと違うか