Skip to content

Instantly share code, notes, and snippets.

@julianpeeters
Created January 13, 2014 18:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julianpeeters/8405121 to your computer and use it in GitHub Desktop.
Save julianpeeters/8405121 to your computer and use it in GitHub Desktop.
Still need daultparam mod if the type is a nested class?
package models
import scala.reflect.macros.Context
import scala.language.experimental.macros
import scala.annotation.StaticAnnotation
import scala.io._
object nestedInnerMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
import Flag._
val result = {
annottees.map(_.tree).toList match {
case q"$mods class $name[..$tparams](..$first)(...$rest) extends ..$parents { $self => ..$body }" :: Nil =>
val nestedInnerVal = q""" val x: String = "hello macro!""""
q"$mods class $name[..$tparams](..$first, $nestedInnerVal)(...$rest) extends ..$parents { $self => ..$body }"
}
}
c.Expr[Any](result)
}
}
object nestedOuterMacro {
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
import c.universe._
import Flag._
val result = {
annottees.map(_.tree).toList match {
case q"$mods class $name[..$tparams](..$first)(...$rest) extends ..$parents { $self => ..$body }" :: Nil =>
val nestedOuterMods = Modifiers(DEFAULTPARAM)
val nestedOuterVal = q"""$nestedOuterMods val myRec: MyRec = MyRec("hello macro!")"""
q"$mods class $name[..$tparams](..$first, $nestedOuterVal)(...$rest) extends ..$parents { $self => ..$body }"
}
}
c.Expr[Any](result)
}
}
class nestedInner extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro nestedInnerMacro.impl
}
class nestedOuter extends StaticAnnotation {
def macroTransform(annottees: Any*) = macro nestedOuterMacro.impl
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment