我在探索Scala宏时遇到了麻烦

我在探索Scala宏时遇到了麻烦,scala,reflection,macros,Scala,Reflection,Macros,当我试图对Scala宏进行操作时,遇到了一些麻烦: object MacroTest { def apply(x: ClassDef): ClassDef = macro impl def impl(c: Context)(x: c.Tree): c.Tree = { import c.universe._ val q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlyde

当我试图对Scala宏进行操作时,遇到了一些麻烦:

object MacroTest {

  def apply(x: ClassDef): ClassDef = macro impl

  def impl(c: Context)(x: c.Tree): c.Tree = {
    import c.universe._
    val q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" = x
    println(tpname)
    x
  }

}
它已成功编译,但出现以下错误:

scala> MacroTest(q"class a")
<console>:30: error: exception during macro expansion:
scala.MatchError: scala.reflect.runtime.`package`.universe.internal.reificationSupport.SyntacticClassDef.apply(scala.reflect.runtime.`package`.universe.NoMods, scala.reflect.runtime.`package`.universe.TypeName.apply("a"), immutable.this.Nil, scala.reflect.runtime.`package`.universe.NoMods, scala.collection.immutable.List.apply[List[Nothing]](immutable.this.Nil), immutable.this.Nil, scala.collection.immutable.List.apply[reflect.runtime.universe.Tree](scala.reflect.runtime.`package`.universe.internal.reificationSupport.ScalaDot.apply(scala.reflect.runtime.`package`.universe.TypeName.apply("AnyRef"))), scala.reflect.runtime.`package`.universe.noSelfType, immutable.this.Nil) (of class scala.reflect.internal.Trees$Apply)
    at MacroTest$.impl(<console>:32)

       MacroTest(q"class a")
                ^

我被搞糊涂了,困在这里了,有人能帮我吗?

你到底想用这个做什么?我只想把一个ClassDef作为参数传递,然后去parse它。你到底想用这个做什么?我只想把一个ClassDef作为参数传递,然后去parse它。
scala> val q"$mods class $tpname[..$tparams] $ctorMods(...$paramss) extends { ..$earlydefns } with ..$parents { $self => ..$stats }" = q"class a"
mods: reflect.runtime.universe.Modifiers = Modifiers(, , Map())
tpname: reflect.runtime.universe.TypeName = a
tparams: List[reflect.runtime.universe.TypeDef] = List()
ctorMods: reflect.runtime.universe.Modifiers = Modifiers(, , Map())
paramss: List[List[reflect.runtime.universe.ValDef]] = List(List())
earlydefns: List[reflect.runtime.universe.Tree] = List()
parents: List[reflect.runtime.universe.Tree] = List(scala.AnyRef)
self: reflect.runtime.universe.ValDef = private val _ = _
stats: List[reflect.runtime.universe.Tree] = List()