Hm... There is a way to use Scala Macro with Scala meta to achieve this. Example:
object VarrApp extends App {
// create a Varr annotation, this in the compile time, will automatically expand the names Vector and generate these variables
@Varr
val names = Vector("a", "b", "c")
println(a) // test
println(b) // test
println(c) // test
}
to achieve this:
1.create a submodule for macros, project structure like:
project:
macros-submodule/src/main/scala/Varr.scala
src/main/scala/VarrApp.scala
2.add Scala meta dependency, like the document, add the paradise compiler plugin, like:
addCompilerPlugin(
"org.scalameta" % "paradise" % "3.0.0-M7" cross CrossVersion.full),
and enable macroparadise in scalacOptions, like:
scalacOptions ++= Seq("-Xplugin-require:macroparadise")
3.Implement Varr annotation, like:
import scala.annotation.StaticAnnotation
import scala.meta._
class Varr extends StaticAnnotation {
inline def apply(defn: Any): Any = meta {
defn match {
case q"val $name = Vector(..$paramss)" => {
val stats = paramss.map {
case i: Lit =>
s"""val ${i.value} = "test"""".parse[Stat].get
}
q"""
..$stats
"""
}
case _ => abort("illegal stats")
}
}
}