Skip to content

Instantly share code, notes, and snippets.

@rirakkumya
Created April 16, 2012 11:28
Show Gist options
  • Save rirakkumya/2397904 to your computer and use it in GitHub Desktop.
Save rirakkumya/2397904 to your computer and use it in GitHub Desktop.
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))
@rirakkumya
Copy link
Author

エラーの出方がちょっと違うか

scala> Right((3,4)).right flatMap {case (x,y) => (x,y)}
<console>:8: error: type mismatch;
 found   : (Int, Int)
 required: Either[?,?]
       Right((3,4)).right flatMap {case (x,y) => (x,y)}
                                                 ^

@hishidama
Copy link

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

とかですか?

@rirakkumya
Copy link
Author

forでエラーになったので、flatmapだとどうなるか試してみただけのコードでした。紛らわしくてすみません。

@rirakkumya
Copy link
Author

この問題解決しました。
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 }
と書いてあります。

@rirakkumya
Copy link
Author

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