I'm writing a macro annotation that is adding a new method to the object it modifies. The method I'm adding needs to know the actual return type of the object's existing methods. It all works fine if they are defined with explicit return types, but for methods with inferred return type I couldn't find any way to retrieve or compute it. Ideally I would need to get the type as an AST, but even as a string is ok. I managed to far to compute it for methods without parameters, but the code breaks when the method has params, as I have no idea how to include the params:
def getDefType(paramss: List[List[ValDef]], tpt: Tree, expr: Tree) = tpt match {
case TypeTree() => c.Expr[Any](c.typecheck(expr)).actualType.toString
case _ => tpt.toString
}
expr is the method body and it doesn't know about the parameters, so the typecheck will fail.
Just to be clear this is compile-time reflection, it is running in the macros universe.
Aucun commentaire:
Enregistrer un commentaire