Scala macros: Is it possible to pass a class body as parameter? -
i'm trying make macro accepts block , uses class description. thought may work:
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}" }
but yields following error when trying use it:
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 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> @ scala.tools.nsc.typechecker.methodsynthesis$methodsynth$basegetter.validate(methodsynthesis.scala:380) @ scala.tools.nsc.typechecker.methodsynthesis$methodsynth$derivedfromvaldef$class.derive(methodsynthesis.scala:330) @ scala.tools.nsc.typechecker.methodsynthesis$methodsynth$basegetter.derive(methodsynthesis.scala:373) @ scala.tools.nsc.typechecker.methodsynthesis$methodsynth$$anonfun$2.apply(methodsynthesis.scala:185) @ scala.tools.nsc.typechecker.methodsynthesis$methodsynth$$anonfun$2.apply(methodsynthesis.scala:185) @ scala.collection.immutable.list.map(list.scala:277) @ scala.tools.nsc.typechecker.methodsynthesis$methodsynth$class.addderivedtrees(methodsynthesis.scala:185)
i have started learning macros i'm not sure if i'm doing wrong or if i'm trying not possible. so, know if i'm far off here?
Comments
Post a Comment