title: Scala macros: Is it possible to pass a class body as parameter?
body: <p>I'm trying to make a macro that accepts a block and uses it as a class description. I thought something like this may work:</p>
  
  <pre><code>class C
  
  def makeC(body: =&gt;Unit): C = macro makeCImpl
  def makeCImpl(c: Context)(body: c.Tree) = { import c.universe._
    val q"..$lines" = body
    q"new C{..$lines}"
  }
  </code></pre>
  
  <p>But this yields the following error when trying to use it:</p>
  
  <pre><code>scala&gt; val c = makeC{var x = 6}
  java.lang.AssertionError: assertion failed: 
    var x: Int = 6
       while compiling: &lt;console&gt;
          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 &lt;console&gt;
                symbol: &lt;none&gt;
     symbol definition: &lt;none&gt; (a NoSymbol)
        symbol package: &lt;none&gt;
         symbol owners: 
             call site: &lt;$anon: C&gt; in package $line35
  
  &lt;Cannot read source file&gt;
      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)
  </code></pre>
  
  <p>I have just started learning about macros so I'm not sure if I'm doing something wrong or if what I'm trying is just not possible. So, does anybody know if I'm too far off here?</p>