As part of a macro I need to inspect the patterns of case definitions.
Is there a way to determine the type of a pattern variable, or even the whole pattern?
Consider the polymorphic class Data
, and a macro which uses a transformer to inspect and transform patterns over Data
values:
case class Data[A](x: String, data: A)
def macroImpl(c: Context)(...) = {
val transformer = new Transformer {
override def transformCaseDefs(trees: List[CaseDef]) = trees map {
case CaseDef(pattern, guard , body) => pattern match {
case pq"Data($string, $data)" => {
// What is the type of $data, i.e., how
// is the type parameter A instantiated?
...
}
}
}
}
...
transformer.transform(...)
}
Is there a way to determine the type of the pattern variable $data, i.e., a way to determine how the type parameter A has been instantiated?
Another question discusses this problem for trees of values and suggests the use of the c.typeCheck
function. Unfortunately, this doesn't seem to work for patterns, since the typeCheck
method throws a TypeCheckException
when applied to the pattern tree of the example above.