Scala macro modify object

爷,独闯天下 提交于 2019-12-13 02:06:15

问题


I have a next macro annotation

 class Foo(obj: String) extends StaticAnnotation {
    def macroTransform(annottees: Any*) = macro MacroImpl.impl
 }

 object MacroImpl {
    def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = {
      import c.universe._

      // i want find `obj` and modify body
    } 

 }

 // usage
 @Foo("pkg.myObject") class SomeClass {}

Is it possible with macro find object by name and modify body of object?


回答1:


This is currently impossible, because macros in Scala can't modify anything outside their scope. E.g. def macros can only rewrite their applications, not the code around, and macro annotations can only rewrite their annottees, not the code around.



来源:https://stackoverflow.com/questions/30555338/scala-macro-modify-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!