且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Scala 宏注释上的类型参数

更新时间:2023-12-01 07:54:28

在宏天堂 2.0.0-SNAPSHOT 中,我们有一个非常棘手的方法来访问宏注释的类型参数(当我们有专用的 API 时,这种情况会有所改善为此,但现在在宏天堂中向 scala-reflect.jar 引入新功能非常困难,因此当前的 API 有点粗糙).

In macro paradise 2.0.0-SNAPSHOT we have quite a tricky way of accessing type parameters for macro annotations (the situation will improve later on when we have dedicated APIs for that, but right now it's very difficult to introduce new functionality to scala-reflect.jar in macro paradise, so the current API is a bit rough).

现在需要在注解类上指定类型参数,而不是在 macroTransform 方法上声明任何类型参数.然后,在宏展开中,访问c.macroApplication,提取传入的类型参数对应的无类型树.然后,按照 处理宏注释时无法访问父成员.

For now it's necessary to specify the type parameter on the annotation class and not to declare any type parameters on the macroTransform method. Then, in macro expansion, access c.macroApplication and extract the untyped tree corresponding to the passed type parameter. Afterwards, do c.typeCheck as described in Can't access Parent's Members while dealing with Macro Annotations.