scalascala-macrosscala-quasiquotes

Scala quasiquote generating parameter default value with backticks


I need to generate:

case class Foo(param: Bar = BarEnum.SomeCaseObject)

But this code:

val term = TermName("BarEnum.SomeCaseObject") 
showCode(q"""case class Foo(val param : Bar = ${term})""") 

outputs the parameter default surrounded by backticks, which doesn't compile:

case class Foo(param: Bar = `BarEnum.SomeCaseObject`)

How can I get the default parameter value output without the backticks?


Solution

  • You are trying to create value with name "BarEnum.SomeCaseObject", that's illegal identifier, thus in backticks.

    You can use Select(Ident(TermName("BarEnum")), TermName("SomeCaseObject")) or (better) q"BarEnum.SomeCaseObject" (assuming that SomeCaseObject is a term).