【发布时间】:2020-10-31 22:21:41
【问题描述】:
我有一个ClassSymbol 并想生成一个零参数方法抛出???。以下是我的尝试:
假设object Test 是我们拥有ClassSymbol 的类型。
我。
val sym = //the ClassSymbol
val tpe = tq"$sym.type"
q"def foo(): $tpe = ???"
结果:
[error] stable identifier required, but Test.type found.
二。
val sym = //the ClassSymbol
val tpe = tq"${sym.name}.type"
q"def foo(): $tpe = ???"
结果:
[error] found : c.universe.TypeName
[error] required: c.universe.TermName
[error] val tpe = tq"${sym.name}.type"
III.
val sym = //the ClassSymbol
val tpe = tq"${TermName(sym.name.toString)}.type"
q"def foo(): $tpe = ???"
结果:
Compiles successfully
所以我最终使用了看起来很吓人的方法III。
是否有“原生”方式在准引用中使用ClassSymbol?
【问题讨论】:
标签: scala metaprogramming abstract-syntax-tree scala-macros scala-quasiquotes