You can use c.prefix.tree
instead of This
.
// macro implementation
import scala.language.experimental.macros
import scala.reflect.macros.Context
import language.dynamics
object MyMacros {
def selectDynamic(c: Context)(name: c.Expr[String]): c.Expr[String] = {
import c.universe._
// I expected `This` to refer a Sample instance, but actually it refers SampleSpec instance...
c.Expr[String](Apply(Select(c.prefix.tree, newTermName("cap")), List(name.tree)))
}
}
// class which uses selectDynamic and macro
class Sample extends Dynamic {
def cap(name: String): String = name.toUpperCase
def selectDynamic(name: String): String = macro MyMacros.selectDynamic
}