Scala allows pattern matching on varargs for unapplySeq
:
case class A(args: String*)
A("a", "b", "c") match {
case A(args @ _*) => args // Seq("a", "b", "c")
}
I want to generate such pattern with macros. How should I do it? A natural thing to try does not work:
scala> pq"x @ _*"
<console>:11: error: illegal start of simple pattern
pq"x @ _*"
^
It is possible to extract the actual type from a q
pattern, however, and recreate the pattern with it:
scala> val q"??? match { case Hello($ident @ $thingy) => }" = q"??? match { case Hello(any @ _*) => }"
ident: reflect.runtime.universe.Name = any
thingy: reflect.runtime.universe.Tree = (_)*
scala> pq"$ident @ $thingy"
res1: reflect.runtime.universe.Bind = (any @ (_)*)
But this is too hacky and I don't want to do it.