Skip to content

Instantly share code, notes, and snippets.

@sadache
Created August 18, 2010 11:51
Show Gist options
  • Save sadache/534438 to your computer and use it in GitHub Desktop.
Save sadache/534438 to your computer and use it in GitHub Desktop.
case class Error[+E,+A](e:Either[E,A]) {
def flatMap[B,EE>:E](f:A => Error[EE,B]):Error[EE,B] ={
Error(e.right.flatMap(a=> f(a).e))
}
def map[B](f:A=>B):Error[E,B]={
Error(e.right.map(f))
}
def toOptionLoggingError():Option[A]={
e.left.map(m => {error(m.toString); m}).right.toOption
}
}
object ErrorUtils{
implicit def eitherToError[E,A](e:Either[E,A]):Error[E,A] = Error(e)
implicit def errorToEither[E,A](e:Error[E,A]):Either[E,A] = e.e
}
scala> import ErrorUtils._
scala> for (i <- Some(1).toRight("Some possible Error");
j <- Right(2):Error[String,Int]) yield i+j
res13: Error[String,Int] = Error(Right(3))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment