Created
March 10, 2013 19:57
-
-
Save derekjw/5130166 to your computer and use it in GitHub Desktop.
macro for wrapping simple values with Future.successful
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
implicit class FutureCompanionW(val underyling: Future.type) extends AnyVal { | |
def smart[A](a: A)(implicit ec: ExecutionContext): Future[A] = macro smartFutureMacroImpl[A] | |
} | |
def smartFutureMacroImpl[A: c.WeakTypeTag](c: Context)(a: c.Expr[A])(ec: c.Expr[ExecutionContext]): c.Expr[Future[A]] = { | |
import c.universe._ | |
a.tree match { | |
case _: Literal | _: Ident => reify(Future successful a.splice) | |
case _ => reify(Future(a.splice)(ec.splice)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment