We have the following typeclass
trait Mapper[A,M[_]]{
def map[B](a:M[A], f:A => B):M[B]
}
For which we would like to provide instances for specific M. A naive implementation would look like the following:
def materializeMapperImpl[A : c.WeakTypeTag,M[_]](c:blackbox.Context)(implicit mTypeTag:c.WeakTypeTag[M[A]]): c.Expr[Mapper[A,M]] = {
import c.universe._
val aType = weakTypeOf[A]
//println(mTypeTag.toString())
c.Expr[Mapper[A,M]]{
q"""
new Mapper[$aType,$mTypeTag]{
def map[B](a1:$mTypeTag, f: $aType => B): $mTypeTag = {
???
}
}
"""
}
}
However, this won't compile because the mTypeTag is not refined in its generic arguments and would rather look as List rather than List[Int] and List[String]. How do we refine the mTypeTag, setting its argument to A and B in the macro?