0

Currently, I tried doing something like the following:

def macroImpl(cx: Context)(...) = {
  new MacroImplementations { val c = cx }
}

but it complains that c in MacroImplementations is of type scala.reflect.macros.runtime.Context, while cx is of type scala.reflect.macros.Context.

What is the difference between those two contexts?

4

1 回答 1

0

我最终得到了以下解决方案 - 它非常难看,但它有效:

import scala.language.experimental.macros
import scala.reflect.macros.Context
import scala.reflect.macros.runtime.{Context => ContextR}
import scala.tools.reflect.MacroImplementations


def putfImpl(cx: Context)(args: cx.Expr[Any]*): cx.Expr[Unit] = {
  val cx2 = cx.asInstanceOf[ContextR]
  val args2 = args.toList.asInstanceOf[List[cx2.Expr[Any]]]
  import cx2.universe._
  val Apply(_, List(Apply(_, partsE))) = cx2.prefix.tree
  val mi = new { val c : cx2.type = cx2 } with MacroImplementations
  val res = mi.macro_StringInterpolation_f(partsE, args2.map(_.tree), cx2.enclosingPosition)
  reify(println(cx2.Expr[String](res).splice)).asInstanceOf[cx.Expr[Unit]]
}
于 2013-06-14T02:58:51.453 回答