I am trying to materialize an instance of the (simplified) trait
trait TC[F[_]] {
def apply[A](fa: F[A]): F[A]
}
using Scala macros. The signature of the macro therefore is
def materialize[F[_]](c: Context)(
implicit fT: c.WeakTypeTag[F[_]]): c.Expr[TC[F]]
Type constructor F[_]
now needs to be applied to the type parameter A
for two reasons:
- To write the signature of
apply
above for a particularF
(likeFoo[A]
) - To inspect the members of the type
Foo[A]
in order to specify an interesting body ofapply
Is there any way to create the type corresponding to the method type parameter A
that can than be used in appliedType
? It appears difficult for me, since the method apply
and its type parameter A
are also just being generated as trees.
I tried to take WeakTypeTag[TC[F]]
as additional argument to the macro call and received the paramter type by
val paramT = wfg.tpe.member("apply": TermName).tpe.typeParams.head.tpe
but then using paramT
in q"... def apply[$paramT] ..."
does result in
java.lang.IllegalArgumentException: can't splice "A" as type parameter
so this appears also to be no solution.