【发布时间】:2022-09-30 08:27:20
【问题描述】:
我想编写一个宏来获取类的属性名称。
但不能在引用语句中使用Symbol 模块。我收到打击错误...
inline def getProps(inline className: String): Iterable[String] = ${ getPropsImpl(\'className) }
private def getPropsImpl(className: Expr[String])(using Quotes): Expr[Iterable[String]] = {
import quotes.reflect.*
val props = \'{
Symbol.classSymbol($className).fieldMembers.map(_.name) // error access to parameter x$2 from
} wrong staging level:
props - the definition is at level 0,
} - but the access is at level 1.
-
而是将类作为类型参数传递
-
但我需要处理类属性的
Symbols。 -
还有一个理由...
-
在这个示例中,我只得到属性的名称,但我也想获得更多关于属性的信息,比如它的类型(
propertySymbol.tree match ...,不能从Class[?]对象中得到它(因为Class[?]不包含输入参数)@cchantep
标签: scala metaprogramming scala-macros scala-3