相关疑难解决方法(0)

宏:路径依赖型推理混淆

我试图简化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: …
Run Code Online (Sandbox Code Playgroud)

scala scala-2.10

3
推荐指数
1
解决办法
404
查看次数

标签 统计

scala ×1

scala-2.10 ×1