Skip to content

Instantly share code, notes, and snippets.

@ochafik
Last active December 13, 2015 22:59
Show Gist options
  • Save ochafik/4988110 to your computer and use it in GitHub Desktop.
Save ochafik/4988110 to your computer and use it in GitHub Desktop.
Example of simplified macro definition that could be made possible by a compiler plugin running before namer and typer
// Absolutely need to annotate return type!
@extend(URL) def secure: URL = macro
reify(new URL(s"https://${self.splice.getHost}${self.splice.getPath}"))
// Gets desugared to:
import scala.language.experimental.macros
import scala.reflect.macros.Context
implicit class secure(self: URL) {
def secure = macro secureImpl.secureImpl
}
object secureImpl {
def secureImpl(c: Context): c.Expr[URL] = {
import c.universe._
val Apply(_, List(selfTree)) = c.prefix.tree
val self = c.Expr[URL](selfTree)
reify(new URL(s"https://${self.splice.getHost}${self.splice.getPath}"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment