什么是Scala的宏观注解的annotees?或者有多少次是宏观施加 [英] What are the annotees of a scala macro annotation? Or how many times is macro applied

查看:153
本文介绍了什么是Scala的宏观注解的annotees?或者有多少次是宏观施加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

花相当一段时间寻遍斯卡拉单证后,我还没有发现这个信息的特定位。或者在某种程度上,我可以很容易地理解或得到任何肯定出至少不措辞。

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

是否有可能让这个注释触发唯一一次与中annotees所有注释类的宏应用?

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只包括直接注明成员+一个值/类型参数一个封闭的定义(类/特征)+的注释成员的伴侣(或为封闭定义的值/类型参数)。

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的宏观注解的annotees?或者有多少次是宏观施加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆