Skip to content

Instantly share code, notes, and snippets.

@derekjw
Created March 10, 2013 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekjw/5130166 to your computer and use it in GitHub Desktop.
Save derekjw/5130166 to your computer and use it in GitHub Desktop.
macro for wrapping simple values with Future.successful
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