Skip to content

Instantly share code, notes, and snippets.

@freekh
Created June 28, 2012 16:36
Show Gist options
  • Save freekh/3012372 to your computer and use it in GitHub Desktop.
Save freekh/3012372 to your computer and use it in GitHub Desktop.
for comprehension error handling
val aOpt: Option[Int] = Some(1)
val c = for {
a <- aOpt.toRight("Missing something in first level!").right
b <- Option(2).toRight("Another failure!").right
} yield {
a + b
}
def onSuccess(i: Int) = i
def onFailure(message: String) = throw new Exception(message)
val r = c.fold(onFailure, onSuccess)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment