The syntax tree of an argument passed to my macro is altered by type inference: notice below that the tree received by identityMacro
is Foo.apply[Int]()
, even though the tree that appears in the source code is just Foo()
.
How could I get back the original tree?
(I don’t even mind the expansion of Foo()
to Foo.apply()
, but I don’t want any inferred type arguments to be added.)
Note: c.untypecheck
only erases the type, but doesn’t remove the inferred type arguments from the tree.
Thanks for your help,
Tomas
$ scala-2.12.2/bin/scala
Welcome to Scala 2.12.2 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_102).
Type in expressions for evaluation. Or try :help.
scala> :silent
scala> :paste
// Entering paste mode (ctrl-D to finish)
import scala.reflect.macros.whitebox.Context
import scala.language.experimental.macros
def identityMacro(c: Context)(arg: c.Tree): c.Tree = {
c.echo(arg.pos, s"Tree passed to identityMacro: ${arg}")
arg
}
def id[A](arg: A): A = macro identityMacro
case class Foo[A]()
// Exiting paste mode, now interpreting.
scala> id[Foo[Int]](Foo())
<console>:17: Tree passed to identityMacro: Foo.apply[Int]()
id[Foo[Int]](Foo())
^
scala>