Skip to content

Instantly share code, notes, and snippets.

@ochafik
Last active December 13, 2015 23:49
Show Gist options
  • Save ochafik/4994437 to your computer and use it in GitHub Desktop.
Save ochafik/4994437 to your computer and use it in GitHub Desktop.
Macro Extensions Plugin Input / Output with macro body
@scalaxy.extension[Any]
def quoted(quote: String): String = macro {
// `c` is defined as the macro Context.
// `quote` and `self` are in scope, defined as `c.Expr[Any]` and `c.Expr[String]`.
println(s"Currently expanding ${c.prefix}.quoted(${quote.tree})")
// `c.universe._` is automatically imported, so `reify` is in scope.
reify({
// Make sure we're not evaluating quote twice, in case it's a complex expression!
val q = quote.splice
q + self.splice + q
})
}
// Transformed AST after the `scalaxy-extensions` compilation phase:
import scala.language.experimental.macros
implicit class scalaxy$extensions$quoted$1(self: Any) {
def quoted(quote: String) = macro scalaxy$extensions$quoted$1.quoted
}
object scalaxy$extensions$quoted$1 {
def quoted(c: scala.reflect.macros.Context)
(quote: c.Expr[String]): c.Expr[String] = {
import c.universe._
val Apply(_, List(selfTree$1)) = c.prefix.tree
val self = c.Expr[Any](selfTree$1)
reify({
// Make sure we're not evaluating quote twice, in case it's a complex expression!
val q = quote.splice
q + self.splice + q
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment