I have a ClassSymbol and want to generate a zero-argument method throwing ???. Here are my attempts:
Assume that object Test is the type we have ClassSymbol of.
I.
val sym = //the ClassSymbol
val tpe = tq"$sym.type"
q"def foo(): $tpe = ???"
Result:
[error] stable identifier required, but Test.type found.
II.
val sym = //the ClassSymbol
val tpe = tq"${sym.name}.type"
q"def foo(): $tpe = ???"
Result:
[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 = ???"
Result:
Compiles successfully
So I ended up using the method III which looks pretty scary.
Is there a "native" way to use ClassSymbol in a quasiquote?
We can save your approach II
This is similar to III but without manual handling strings.
Also don't forget that besides quasiquotes you can always build trees with manual parsing
Regarding the "native" way, now it's better to use a
ModuleSymbolor
rather than
ClassSymbolor
Testing:
I used runtime reflection but for macros it's similar.
If you already have a
ClassSymbolit can be converted to aModuleSymbolor
or
Get the module symbol, given I have the module class, scala macro