Last active
August 29, 2015 14:24
-
-
Save smithjessk/ef507791b0eff36d00ac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.annotation.StaticAnnotation | |
import scala.language.experimental.macros | |
import scala.reflect.macros.Context | |
import scala.reflect.ClassTag | |
import scala.reflect.runtime.universe.Flag._ | |
// Add constructor arguments here. | |
class expand extends StaticAnnotation { | |
def macroTransform(annottees: Any*) = macro Expander.expand_impl | |
} | |
object Expander { | |
def f[T](v: T)(implicit ev: ClassTag[T]) = { | |
ev.toString | |
} | |
def expand_impl(c: Context)(annottees: c.Expr[Any]*) = { | |
import c.universe._ | |
annottees.map(_.tree) match { | |
case List(q"case class $name(...$paramss)") => | |
val nameModified: TypeName = name match { | |
case TypeName(s: String) => TypeName(s + "Modified") | |
case _ => c.abort(c.enclosingPosition, "Not a type name") | |
} | |
val paramssModified = paramss(0) :+ ValDef(Modifiers(Flag.PARAMACCESSOR | Flag.CASEACCESSOR), | |
TermName("k"), Int.typeSymbol, EmptyTree) | |
println(paramssModified) | |
c.Expr[Any]( | |
q""" | |
case class $name(...$paramss) | |
class $nameModified(...$paramssModified) | |
""" | |
) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment