I'm trying to get an implicit parameter to be generated by a macro. When requesting the StructTypeInfo implicit, there is a compiler error, and log-implicits shows:
[info] Test.scala:29: materializeCaseClassSchemaType is not a valid implicit value for org.apache.hadoop.hive.serde2.typeinfo.StructTypeInfo because:
[info] hasMatchingSymbol reported error: We cannot enforce T is a case class, either it is not a case class or this macro call is possibly enclosed in a class.
The way I got it to compile is if I wrap it in a parameterized class:
trait SchemaWrapper[T] {
def schema: StructTypeInfo
}
Here some relevant code (let me know if I left something out):
// Implicits, one works, one doesn't
object MacroImplicits {
// Works:
implicit def materializeCaseClassSchemaTypeWrapper[T]: SchemaWrapper[T] = macro SchemaTypeImpl.caseClassSchemaTypeWrapper[T]
// Doesn't:
implicit def materializeCaseClassSchemaType[T]: StructTypeInfo = macro SchemaTypeImpl.caseClassSchemaType[T]
}
// Implementation (most of it probably not relevant)
// Another version returns StructTypeInfo directly without the wrapper
object SchemaTypeImpl {
def caseClassSchemaTypeWrapper[T](c: Context)(implicit T: c.WeakTypeTag[T]): c.Expr[SchemaWrapper[T]] = {
import c.universe._
if (!IsCaseClassImpl.isCaseClassType(c)(T.tpe))
c.abort(c.enclosingPosition, s"""We cannot enforce ${T.tpe} is a case class, either it is not a case class or this macro call is possibly enclosed in a class.""")
...
}
}
Interestingly enough, if I explicitly pass materializeCaseClassSchemaType[MyCaseClass]
to the function in the implicit spot, it works fine. Why do I need the wrapper, and can I get rid of it?