Skip to content

Instantly share code, notes, and snippets.

@trane
Created January 28, 2017 00:00
Show Gist options
  • Save trane/71c5844b3b0e13f46dad4b212fbc1f31 to your computer and use it in GitHub Desktop.
Save trane/71c5844b3b0e13f46dad4b212fbc1f31 to your computer and use it in GitHub Desktop.
object OptionUtils {
def fromOption[A](opt: Option[A])(onNone: => Throwable): Future[A] =
Future.successful(opt).map(_.getOrElse(throw onNone))
def fromOption[A](opt: Option[A])(onNone: => A): Future[A] =
Future.successful(opt).map(_.getOrElse(onNone))
}
class OptionOps[A](val self: Option[A]) {
def toFuture(onNone: => Throwable): Future[A] = OptionUtils.fromOption(self)(onNone)
def toFuture(onNone: => A): Future[A] = OptionUtils.fromOption(self)(onNone)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment