宏:路径依赖型推理混淆

kir*_*uku 3 scala scala-2.10

我试图简化AST的创建,但得到了一个奇怪的错误消息:

case class Box(i: Int)
object M {
  import language.experimental.macros
  import scala.reflect.makro.Context
  case class meth(obj: String, method: String)(implicit val c: Context) {
    import c.universe._

    def apply(xs: Tree*) =
      Apply(Select(Ident(obj), newTermName(method)), xs.toList)
  }

  def box(n: Int): Box = macro boxImpl

  def boxImpl(c: Context)(n: c.Expr[Int]): c.Expr[Box] = {
    import c.universe._
    implicit val cc: c.type = c
    n.tree match {
      case arg @ Literal(Constant(_)) =>
        meth("Box", "apply").apply(arg)
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

错误:

<console>:26: error: type mismatch;
 found   : c.universe.Literal
 required: _2.c.universe.Tree where val _2: M.meth
 possible cause: missing arguments for method or constructor
               meth("Box", "apply").apply(arg)
                                          ^
Run Code Online (Sandbox Code Playgroud)

是否有可能将正确的类型推断为类meth?或者有问题的解决方法吗?

编辑:基于@retronyms的答案我得到了这个工作:

object M {
  import language.experimental.macros
  import scala.reflect.makro.Context

  def meth(implicit c: Context) = new Meth[c.type](c)

  class Meth[C <: Context](val c: C) {
    import c.universe._

    def apply(obj: String, method: String, xs: Tree*) =
      Apply(Select(Ident(obj), newTermName(method)), xs.toList)
  }

  def box(n: Int): Box = macro boxImpl

  def boxImpl(c: Context)(n: c.Expr[Int]): c.Expr[Box] = {
    import c.universe._
    implicit val cc: c.type = c
    n.tree match {
      case arg @ Literal(Constant(_)) =>
        c.Expr(meth.apply("Box", "apply", arg))
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

ret*_*nym 9

目前不允许构造函数具有依赖方法类型(SI-5712).雷达需要修复,希望是2.10.1或2.11.

与此同时,您可以遵循我在宏观实现中使用的模式 来重用宏实现中的代码.