我想在下面的selectDynamic宏实现中使用Sample#cap(String)

可能吗?

// 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
  }
}

最佳答案

您可以使用c.prefix.tree代替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
}

关于scala - 在宏实现中为selectDynamic调用实例方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15653550/

10-10 13:53