マクロによって生成される暗黙のパラメーターを取得しようとしています。StructTypeInfo 暗黙を要求すると、コンパイラ エラーが発生し、log-implicits が次のように表示されます。
[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.
コンパイルする方法は、パラメーター化されたクラスでラップする場合です。
trait SchemaWrapper[T] {
def schema: StructTypeInfo
}
ここに関連するコードがいくつかあります(何かを省略した場合はお知らせください):
// 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.""")
...
}
}
興味深いことにmaterializeCaseClassSchemaType[MyCaseClass]
、暗黙的なスポットで関数に明示的に渡すと、正常に動作します。なぜラッパーが必要なのですか? それを取り除くことはできますか?