Created
June 28, 2012 16:36
-
-
Save freekh/3012372 to your computer and use it in GitHub Desktop.
for comprehension error handling
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
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