Skip to content

Instantly share code, notes, and snippets.

@kmizu
Forked from makotan/gist:1476310
Created December 14, 2011 13:20
Show Gist options
  • Save kmizu/1476542 to your computer and use it in GitHub Desktop.
Save kmizu/1476542 to your computer and use it in GitHub Desktop.
Either[L,Either[L,R]]のように多段になる場合にRだけを取得する (改変版)
def eith[R](arg: Either[Throwable, R] ) : R = {
/* Right(o:Either[Throwable, R]) パターンは
* erasureのせいで警告が出る点に注意 */
arg match {
case Left(e) => throw e
case Right(o:Either[Throwable, R]) => eith(o)
case Right(o:R) => o
}
}
println(eith(Right(1)))
val ex: Either[Throwable, Either[Throwable, Int]] = Right(Right(1))
println(eith(ex))
try {
eith(Left(new RuntimeException("oops!")))
} catch { case ex: RuntimeException =>
println(ex.getMessage())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment