我想Sample#cap(String)
在下面的宏实现中使用 selectDynamic。
可能吗?
// macro implementation
import scala.language.experimental.macros
import scala.reflect.macros._
object MyMacros {
def selectDynamic(c: Context)(name: c.Expr[String]): c.Expr[String] = {
// I expected `This` to refer a Sample instance, but actually it refers SampleSpec instance...
c.Expr[String](Apply(TypeApply(Select(This(tpnme.EMPTY), newTermName("cap")), List(Ident(typeTag[String].tpe.typeSymbol))), List(name.tree)))
}
}
// class which uses selectDynamic and macro
import scala.language.dynamics
class Sample extends Dynamic {
def cap(name: String): String = name.toUpperCase
def selectDynamic(name: String): String = macro MyMacros.selectDynamic
}
class SampleSpec extends FlatSpec with ShouldMatchers {
it should "call dynamic" in {
val sample = new Sample
sample.foo should equal("FOO") // value cap is not a member of SampleSpec
}
}