3
내가하는 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)
}
}
}
오류 :
<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)
^
가 올바른 유형을 추론하는 것이 가능 수업에 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))
}
}
}