scalascala-macrosscala-2.13scala-2.12

Scala Macro Type Args Introspection on Case Class


I've got a library that generates mappings for case classes at compile-time and it works great, unless I pass in a generic type like: Foo[Bar] (see https://github.com/outr/fabric/blob/master/core/shared/src/main/scala-2/fabric/rw/RWMacros.scala#L32). When I call the Macro with caseClass[Foo[Baz]] the typeSignature represents Bar, not Baz. I can see in the tpe that that at compile-time it is Foo[Baz], but I can't figure out how to map from the Bar generic type to the Baz in the typeArgs list.


Solution

  • Thank you to @zygfryd on Gitter.im! He pointed me to asSeenFrom and I was able to update the code with val returnType = tpe.decl(name).typeSignature.asSeenFrom(tpe, tpe.typeSymbol.asClass) and now it resolves correctly to the generic type.