Created
January 13, 2014 18:09
-
-
Save julianpeeters/8405121 to your computer and use it in GitHub Desktop.
Still need daultparam mod if the type is a nested class?
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
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