I'm trying to use the fields of a type in a macro, and pass the symbol or typesigs for the fields to a method so I can do some operations that require concrete information about the type.
I have code like this (played about with variations):
object Macros {
import scala.reflect.runtime.universe._
def foo(t: Symbol) : String = t.name.decoded
def materializeWriterImpl[T: c.WeakTypeTag](c: Context): c.Expr[List[String]] = {
import c.universe._
val tpe = weakTypeOf[T]
val fields = tpe.declarations.collectFirst {
case m: MethodSymbol if m.isPrimaryConstructor => m
}.get.paramss.head
c.Expr[List[String]] { q"""
val q = $fields
val names = q.map(Macros.foo)
List(names)
"""
}
}
}
The error I get is
Error:(53, 24) Can't unquote List[c.universe.Symbol], consider using .. or providing an implicit instance of Liftable[List[c.universe.Symbol]]
val names = foo($fields)
^
So, perhaps its not possible to lift symbol / type using qquotes. But I can see methods to do this in StandardLiftableApi
:
implicit def liftScalaSymbol : U#Liftable[scala.Symbol]
implicit def liftType[T <: U#Type] : U#Liftable[T]
I can get this working if I pass strings, just as a test, but I really need something more substantial passed out.
Following your comments about what you're actually trying to achieve, here's a rather comprehensive example that shows how to generate schemas (for a totally made up schema description language).
And some REPL test: