Skip to content

Instantly share code, notes, and snippets.

@makotan
Created December 14, 2011 11:59
Show Gist options
  • Save makotan/1476310 to your computer and use it in GitHub Desktop.
Save makotan/1476310 to your computer and use it in GitHub Desktop.
Either[L,Either[L,R]]のように多段になる場合にRだけを取得する
def eith[R](arg: Either[Throwable, Any] ) : R = {
arg match {
case Left(e) => throw e
case Right(o:Either[Throwable, Any]) => eith(o)
case Right(o:R) => o
}
}
@kmizu
Copy link

kmizu commented Dec 14, 2011

この定義だと、eith Stringのようにして呼び出すか、
val x: Int = eith(eitherObj)
のように、R の型をうまく推論できるパターンでないと、R = Nothingとして推論されてしまい不便なので、ちょっと改良版?を作ってみました。

https://gist.github.com/1476542

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment