Skip to content

Instantly share code, notes, and snippets.

@runarorama
Created October 8, 2014 21:28
Show Gist options
  • Save runarorama/3706a073fb2df0d2b29b to your computer and use it in GitHub Desktop.
Save runarorama/3706a073fb2df0d2b29b to your computer and use it in GitHub Desktop.
import scala.reflect.macros.Context
import scala.language.experimental.macros
import scala.annotation.StaticAnnotation
object helloMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
import Flag._
val args = c.prefix.tree match {
case q"new $name($m)" => c.eval(c.Expr[Map[String, String]](c.resetAllAttrs(m)))
}
println(args)
val result = {
annottees.map(_.tree).toList match {
case q"abstract class $name extends ..$parents { ..$body }" :: Nil =>
q"""
abstract class $name extends ..$parents {
def hello: ${typeOf[String]} = "hello"
..$body
}
"""
}
}
c.Expr[Any](result)
}
}
class hello(m: Map[String, String]) extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro helloMacro.impl
}
class DoesThisWork(m: Map[String, String]) {
@hello(m) class T
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment