下面的宏生成case类Person并返回该类的实例。它使用whitebox宏,这样编译器就可以推断出类型Person。这允许宏客户端调用p.name,即使这个字段是在宏中生成的。
import scala.language.experimental.macros
class MacroDefs(val c: scala.reflect.macros.whitebox.Context) {
import c.universe._
def createCaseImpl(): c.Expr[Product] = {
val code: Tree = q""" case class Person(name:String); new Person("Joe") """
c.Expr(code)
}
}
object Macros {
def createCase(): Product = macro MacrosDef.createCaseImpl
}
object Test {
def main(args: Array[String]) {
val p = Macros.createCase()
println("Name: " + p.name)
}
}代码可以工作,但是编译器使用结构类型访问p.name,从下面的警告消息中可以看到(我通过反编译生成的字节码来确认它):
Warning:(5, 54) inferred existential type Person forSome { type Person <: Product with Serializable{val name: String; def copy(name: String): Person; def copy$default$1: String @scala.annotation.unchecked.uncheckedVariance} }, which cannot be expressed by wildcards, should be enabled
by making the implicit value scala.language.existentials visible.
val c = Macros.createCase()
^由于结构类型依赖于Java反射,所以我关注性能。
我的问题是,是否有更好的方法这样做,使编译器将使用标准的方法调用,而不是结构类型和反射。我使用Scala2.11.6。
第二个小问题: Intellij似乎不能很好地处理whitebox宏,并将对p.name的访问标记为无效,表示该字段是未知的,尽管它将使用scalac编译和运行。有办法让Intellij知道白盒宏吗?
发布于 2015-05-06 09:22:30
按照@MartinRing和@TravisBrown的建议,我使用了宏注释来解决问题。这篇文章也非常有用:http://www.47deg.com/blog/scala-macros-annotate-your-case-classes。我把代码贴在这里供将来参考。
在客户机代码中,我注释了一个包装器对象,然后这个对象将由宏展开,以包含一个新的case类Person和这个类的一个实例。与使用结构类型和反射的匿名类型提供程序解决方案相反,生成的代码使用标准方法调用。
@personGenerator object Wrapper
object Main {
def main(args: Array[String]) {
println("Name: " + Wrapper.thePerson.name)
val other = new Wrapper.Person("Joan")
println("Other: " + other.name)
}
}以下是注释和宏的实现:
class personGenerator extends StaticAnnotation {
def macroTransform(annottees: Any*): Any = macro MacroDefs.personGenerator_impl
}
class MacroDefs(val c: scala.reflect.macros.whitebox.Context) {
import c.universe._
def personGenerator_impl(annottees: c.Expr[Any]*): c.Expr[Any] = {
annottees.map(_.tree) match {
case List(q"object $name { ..$body }") =>
val code = q"""
object $name {
..$body
case class Person(name:String)
def thePerson = new Person("Joe")
}
"""
c.Expr[Any](code)
}
}
}https://stackoverflow.com/questions/30046230
复制相似问题