scala宏与import语句不起作用
我正在诠释一个特征,例如:
@ScreenModel trait TestTrait { ....... } 然后我得到了@ScreenModel的实现,例如: def impl(c: whitebox.Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { import c.universe._ val output : List[Tree] = annottees.map(_.tree) match { case(cd @ q"$mods trait $name[..$tparams] extends ..$parents { ..$body }") :: Nil => val compObjVar = s"""{ import models.presentation.blocks.BlockModel; class AAAA { class BBBB { } } }"""; val rawTree=c.parse(compObjVar) val merged = q"{..$rawTree}"; merged :: Nil case _ => c.abort(c.enclosingPosition,"Invalid test target") } c.Expr[Any](q"..$output") } 所以,我得到了: “没有伴侣的顶级课程只能扩展为同名课程或者成块” 但是,如果我在AAA类中的AAA类之前移动导入,那么它可以工作: val compObjVar = s"""{ class AAAA { import models.presentation.blocks.BlockModel; class BBBB { } } }"""; 为什么? 解决方法
它打破了
documentation的这条规则:
这意味着如果要使用宏注释转换类或特征,则必须扩展为具有相同名称的类或具有相同名称的伴随的类.也就是说,我们保留正在扩展的树的根. 如果我们尝试扩展Ainto类: import package.name class A 我们正在扩展的树的根不再是A类的根,而是包含类和新导入的树的根,但它是什么?它不能没有.减少可能的扩展量可以降低插件的复杂性,从而降低可能出错的数量. 如果必须添加导入,则可以在类中添加.如果导入需要同时出现在类和伴随对象中,则可以将它们分解到自己的Tree中进行拼接. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |