Question

Suppose I have a macro implementation

def testImpl[T](c:Context)(implicit wtt:c.WeakTypeTag[T]):c.Tree = {
    import c.universe._

    def info(msg: Any) = c.info(c.enclosingPosition, msg.toString, true)

    val parameter = wtt.tpe.member(TermName("f")).paramLists(0)(0)

    info(parameter.typeSignature)

    q"{}"
}

And a macro definition

def test[T]:Unit = macro DerivingImpl.testImpl[T]

This macro finds function f in its type parameter, and prints information about its first parameter's type.

Now if I use this macro like this

trait Trait[A] {
    def f(x:A): Int
}

test[Trait[Int]]

I get A printed. I would like to get Int. I understand that calling member returns the method symbol which does not have information about the concrete applied type. So, what is the correct way to find the actual type of the parameter?

Thank you.

Was it helpful?

Solution

val f = wtt.tpe.member(TermName("f"))
val MethodType(parameter :: Nil, _) = f.typeSignatureIn(wtt.tpe)
info(parameter.typeSignature)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top