Follow-up on our discussion on NativeLibs4Java's mailing list, where I gave an example of such a compiler plugin (which expands @extend(Int) def foo = blah
into implicit class foo(self: Int) extends AnyVal { def foo = blah }
).
I've written a more elaborated plugin that expands these definitions into... macros (giving macro-expandable extensions / "pimps", with no runtime dependency!).
Given:
@extend(Any) def quoted(quote: String): String = quote + self + quote
It expands to:
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(quote.splice + self.splice + quote.splice)
}
}
}