-
-
Save kmizu/1476542 to your computer and use it in GitHub Desktop.
Either[L,Either[L,R]]のように多段になる場合にRだけを取得する (改変版)
This file contains hidden or 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
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