I have asked this question in the metaprogramming discord channel and the Github discussion forum, but have had no response. So I am trying anew here.
Assume I have the following code:
val s1 = ConstArray.testUnapply(Array(1, 2, 3))
I would like to match that array in the testUnapply(Array(1, 2, 3))
macro. The following match using quotes work:
(x: Expr[Array[T]]) match
case '{ scala.Array.apply($a:Int, $b:Int, $c:Int): scala.Array[Int]} =>
println(s"(a, b, c) = ($a, $b, $c)")
???
However, I need to deal with a variable number of arguments. So I tried many variations that include:
case '{ scala.Array.apply(${Varargs(Exprs(elems))}: Seq[Int])} =>
=>
Some(Array(elems*).asInstanceOf[Array[T]])
???
None of these work. I have also noticed that the following fails:
case '{ scala.Array.apply[Int]($a:Int, $b:Int, $c:Int): scala.Array[Int]} =>
println(s"(a, b, c) = ($a, $b, $c)")
???
Why does this fail? Do I have to add something related to to ClassTag
? The following also fails:
val fct = summon[FromExpr[ClassTag[Int]]]
(x: Expr[Array[T]]) match
case '{ scala.Array.apply( ${varArgs} : Seq[Int])(using $fct) } =>
???
Note that a show prints :
scala.Array.apply(1, 2, 3)
and the structure is:
Apply(Select(Ident("Array"), "apply"), List(Literal(IntConstant(1)), Typed(Repeated(List(Literal(IntConstant(2)), Literal(IntConstant(3))), Inferred()), Inferred())))
I have used the same technique for Seq
and List
with no problems.
Any help is appreciated.
TIA