You could use method showRaw
to get Tree
structure like this:
import reflect.runtime.universe._
showRaw{ reify{ List(1, 2, 3) } }
// Expr(Apply(Select(Ident(scala.collection.immutable.List), newTermName("apply")), List(Literal(Constant(1)), Literal(Constant(2)), Literal(Constant(3)))))
reify
returns Expr
.
Result with indentations:
Expr(
Apply(
Select(
Ident(scala.collection.immutable.List),
newTermName("apply")
),
List(
Literal(Constant(1)),
Literal(Constant(2)),
Literal(Constant(3))
)
)
)
So you could deconstruct this tree:
val vval = reify{ List(1, 2, 3) }
val l =
vval.tree match {
case Apply(_, l) => l
}
// List[reflect.runtime.universe.Tree] = List(1, 2, 3)
val ints =
l map {
case Literal(Constant(i: Int)) => i
}
// List[Int] = List(1, 2, 3)
Note that it will work only in case when your method test
is called like test(List(1, 2, 3))
. It will fail on val l = List(1, 2, 3); test(l)
. It will fail even on test(List(1+1))
.
The only useful application for such parameters extraction is macros-based string interpolation like this.