问题描述
在花了相当长的时间搜索 scala 文档后,我还没有找到这个特定的信息.或者至少没有以我可以轻松理解或确定的方式表达.
After spending quite sometime searching through scala documentations I have not found this particular bit of information. Or at least not phrased in a way I could easily understand or get any certainty out of.
我有这个注释:
class MyAnnotation extends StaticAnnotation {
def macroTransform(annotees: Any*) = macro myImpl
}
我已经在两个或多个这样的类中使用过它:
And I have used it on two or more classes like this:
@MyAnnotation
class One {}
@MyAnnotation
class Two {}
我想知道 annotees
是否会包含这两个类,或者宏是否会被执行两次(每个注释实例一个).我会有吗?
I would like to know if the annotees
will contain both the classes or if the macro will be executed twice (one for each instance of the annotation). Will I have?
annotess.map(_tree).toList == List(oneClassDef /*classdef of One*/, twoClassDef /*classdef of Two*/)
> true
是否有可能使注释同时触发宏的一个应用程序以及注释中的所有注释类?
Is it possible to make it so that the annotation trigger only one application of the macro with all the annotated classes in the annotees at once?
推荐答案
注解者只包括直接注解的成员 + 一个值/类型参数的封闭定义(类/特征)+注解成员(或值/类型参数的封闭定义).
Annottees only include the directly annotated member + an enclosing definition (class/trait) for a value/type parameter + a companion for the annotated member (or for the enclosing definition for a value/type parameter).
不幸的是,在 scalac 的当前命名器/打字器架构中几乎不可能实现您的请求(并且,据了解,在 dotc 中也是如此),所以我建议一个解决方法 - 注释一个包含所有内容的定义您要处理的类.
Unfortunately, it's virtually impossible to implement your request in the current namer/typer architecture of scalac (and, to the best of knowledge, in dotc as well), so I'd suggest a workaround - annotating a definition that encloses all the classes that you want to process.
这篇关于scala 宏注释的注释是什么?或者宏应用了多少次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!