gpt4 book ai didi

Scala宏天堂: How do I get an annotation's arguments using macro annotations?

转载 作者:行者123 更新时间:2023-12-02 17:42:43 30 4
gpt4 key购买 nike

假设我想使用宏注释来复制@specialized(Int) 之类的注释——我知道这很疯狂。像这样的东西:

class expand(expanded: Any*) extends Annotation with StaticAnnotation {
def macroTransform(annottees: Any*) = macro expand.expandImpl
}

object expand {
def expandImpl(c: Context)(annottees: c.Expr[Any]*):c.Expr[Any] = {
// would like to be able to get access to the "expanded" args above.
???
}
}

// Usage:
def foo[@expand(Int) T] = 4

有什么方法可以访问注释的参数(示例中的 Int)?

最佳答案

看看c.prefix。它将包含一个对应于 new expand(Int) 的树。

另一个选项是 c.macroApplication,它将是 new expand(Int).macroTransform(...)

关于Scala宏天堂: How do I get an annotation's arguments using macro annotations?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18691038/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com