forked from scalamacros/paradise
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import scala.reflect.macros.Context | ||
import scala.language.experimental.macros | ||
import scala.annotation.StaticAnnotation | ||
|
||
object shoveMacro { | ||
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { | ||
import c.universe._ | ||
import Flag._ | ||
val Apply(Select(New(AppliedTypeTree(_, List(victim))), _), _) = c.prefix.tree | ||
val result = { | ||
annottees.map(_.tree).toList match { | ||
case ValDef(mods, name, tpt, rhs) :: Nil => | ||
ClassDef( | ||
Modifiers(IMPLICIT), | ||
newTypeName(s"${victim}With$name"), | ||
List(), | ||
Template( | ||
List(Select(Ident(newTermName("scala")), newTypeName("AnyRef"))), | ||
emptyValDef, | ||
List( | ||
ValDef(Modifiers(PRIVATE | LOCAL), newTermName("x"), victim, EmptyTree), | ||
DefDef( | ||
Modifiers(), | ||
nme.CONSTRUCTOR, | ||
List(), | ||
List(List(ValDef(Modifiers(PARAM), newTermName("x"), victim, EmptyTree))), | ||
TypeTree(), | ||
Block(List(Apply(Select(Super(This(tpnme.EMPTY), tpnme.EMPTY), nme.CONSTRUCTOR), List())), c.literalUnit.tree) | ||
), | ||
ValDef(mods, name, tpt, rhs) | ||
) | ||
) | ||
) | ||
} | ||
} | ||
c.Expr[Any](result) | ||
} | ||
} | ||
|
||
class shove[A] extends StaticAnnotation { | ||
def macroTransform(annottees: Any*) = macro shoveMacro.impl | ||
} | ||
|
||
package pkg { | ||
class shove extends StaticAnnotation { | ||
def macroTransform(annottees: Any*) = macro shoveMacro.impl | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import org.scalatest.FunSuite | ||
import scala.reflect.runtime.universe._ | ||
|
||
class TypeArgs extends FunSuite { | ||
test("macro annotations with type args expand") { | ||
@shove[Int] val description = "I’m an Int!" | ||
@shove[String] val bar = "I’m a String!" | ||
assert(5.description === "I’m an Int!") | ||
assert("foo".bar === "I’m a String!") | ||
} | ||
} |