Scala宏:是否可以将类主体作为参数传递?

时间:2016-06-10 21:55:09

标签: scala macros scala-2.11

我试图创建一个接受块的宏并将其用作类描述。我觉得这样的事情可能有用:

class C

def makeC(body: =>Unit): C = macro makeCImpl
def makeCImpl(c: Context)(body: c.Tree) = { import c.universe._
  val q"..$lines" = body
  q"new C{..$lines}"
}

但是在尝试使用它时会产生以下错误:

scala> val c = makeC{var x = 6}
java.lang.AssertionError: assertion failed: 
  var x: Int = 6
     while compiling: <console>
        during phase: globalPhase=typer, enteringPhase=namer
     library version: version 2.11.8
    compiler version: version 2.11.8
  reconstructed args: -deprecation -feature -bootclasspath /usr/lib/jvm/jdk1.8.0_31/jre/lib/resources.jar:/usr/lib/jvm/jdk1.8.0_31/jre/lib/rt.jar:/usr/lib/jvm/jdk1.8.0_31/jre/lib/sunrsasign.jar:/usr/lib/jvm/jdk1.8.0_31/jre/lib/jsse.jar:/usr/lib/jvm/jdk1.8.0_31/jre/lib/jce.jar:/usr/lib/jvm/jdk1.8.0_31/jre/lib/charsets.jar:/usr/lib/jvm/jdk1.8.0_31/jre/lib/jfr.jar:/usr/lib/jvm/jdk1.8.0_31/jre/classes:/home/nicolas/.ivy2/cache/org.scala-lang/scala-library/jars/scala-library-2.11.8.jar -unchecked -classpath /home/nicolas/Development/Programs/Scala/scalajs-test/target/scala-2.11/classes:/home/nicolas/.ivy2/cache/org.scala-lang/scala-compiler/jars/scala-compiler-2.11.8.jar:/home/nicolas/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.8.jar:/home/nicolas/.ivy2/cache/org.scala-lang.modules/scala-xml_2.11/bundles/scala-xml_2.11-1.0.4.jar:/home/nicolas/.ivy2/cache/org.scala-lang.modules/scala-parser-combinators_2.11/bundles/scala-parser-combinators_2.11-1.0.4.jar:/home/nicolas/.ivy2/cache/org.scala-js/scalajs-library_2.11/jars/scalajs-library_2.11-0.6.9.jar:/home/nicolas/.ivy2/cache/org.scala-js/scalajs-dom_sjs0.6_2.11/jars/scalajs-dom_sjs0.6_2.11-0.9.0.jar:/home/nicolas/.ivy2/cache/org.scala-lang/scala-compiler/jars/scala-compiler-2.11.8.jar:/home/nicolas/.ivy2/cache/org.scala-lang/scala-reflect/jars/scala-reflect-2.11.8.jar:/home/nicolas/.ivy2/cache/org.scala-lang.modules/scala-xml_2.11/bundles/scala-xml_2.11-1.0.4.jar:/home/nicolas/.ivy2/cache/org.scala-lang.modules/scala-parser-combinators_2.11/bundles/scala-parser-combinators_2.11-1.0.4.jar:/home/nicolas/.ivy2/cache/jline/jline/jars/jline-2.12.1.jar -Yrepl-sync -Xplugin:/home/nicolas/.ivy2/cache/org.scala-js/scalajs-compiler_2.11.8/jars/scalajs-compiler_2.11.8-0.6.9.jar

  last tree to typer: Ident(C)
       tree position: line 18 of <console>
              symbol: <none>
   symbol definition: <none> (a NoSymbol)
      symbol package: <none>
       symbol owners: 
           call site: <$anon: C> in package $line35

<Cannot read source file>
    at scala.tools.nsc.typechecker.MethodSynthesis$MethodSynth$BaseGetter.validate(MethodSynthesis.scala:380)
    at scala.tools.nsc.typechecker.MethodSynthesis$MethodSynth$DerivedFromValDef$class.derive(MethodSynthesis.scala:330)
    at scala.tools.nsc.typechecker.MethodSynthesis$MethodSynth$BaseGetter.derive(MethodSynthesis.scala:373)
    at scala.tools.nsc.typechecker.MethodSynthesis$MethodSynth$$anonfun$2.apply(MethodSynthesis.scala:185)
    at scala.tools.nsc.typechecker.MethodSynthesis$MethodSynth$$anonfun$2.apply(MethodSynthesis.scala:185)
    at scala.collection.immutable.List.map(List.scala:277)
    at scala.tools.nsc.typechecker.MethodSynthesis$MethodSynth$class.addDerivedTrees(MethodSynthesis.scala:185)

我刚刚开始学习宏,所以我不确定我做错了什么,或者我尝试的是不可能的。那么,有人知道我离这儿太远了吗?

0 个答案:

没有答案