Last active
August 29, 2015 14:06
-
-
Save cocodrino/75e89c39fb4818fe0e58 to your computer and use it in GitHub Desktop.
Future[Option[_] or Future[Option[_]]
This file contains 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
import scala.reflect.runtime.universe._ | |
//in A perfect world : without type erasure | |
implicit class SuperFuture (a : Future[Option[_]]){ | |
def or (b : Future[Option[_]]) ={ | |
a match { | |
case a1 : Future[Some[_]] => a1 | |
case _ => b | |
} | |
} | |
} | |
def m[B <:Option[_]](a : B)(implicit ev: TypeTag[B]) = ev.tpe | |
//in the scala world : handling type erasure... | |
implicit class SuperFuture3[B <: Option[_] : TypeTag](a : Future[B]) { | |
def or (b : Future[Option[_]]) ={ | |
typeOf[B] match{ | |
case t if t<:< typeOf[Some[_]] => a | |
case _ => b | |
} | |
} | |
} | |
//or | |
implicit class SuperFuture2 [B <: Option[_]](a : Future[B]) | |
(implicit tag : TypeTag[B]){ | |
def or (b : Future[Option[_]]) ={ | |
typeOf[tag.type] match{ | |
case t if t<:< typeOf[Some[_]] => a | |
case _ => b | |
} | |
} | |
} | |
Future[None] or Future [None] or Future[Some(2)] ---> Future[Some(2)] | |
val q = Future{ | |
Some(5) | |
} | |
val w = Future{ | |
Some(10) | |
} | |
val x : Future[Option[Int]]= Future{ | |
None | |
} | |
(x or w).onComplete(println(_)) //Success(Some(5)) | |
nice!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment