19

I am writing a macro in Scala, but when I call it I get an error message saying "Double does not take parameters". Clearly there is something wrong with how the macro builds the AST. So how can I see the expanded macro? Is there a way to call the macro implementation at runtime?

Kim Stebel
  • 41,826
  • 12
  • 125
  • 142

2 Answers2

25

Provide -Ymacro-debug-lite or -Ymacro-debug-verbose option to the compiler.

Off the top of my head, detalization of printed ASTs is governed by -Yshow-trees-compact, -Yshow-trees-stringified, -Xprint-types, -uniqid and -Yshow-symkinds. You can find other gems by running scala -X and scala -Y (or inspecting the sources of scala settings at https://github.com/scala/scala/blob/2.10.x/src/compiler/scala/tools/nsc/settings/ScalaSettings.scala).

Also, despite being essentially a macro, reification has its own tracing mechanism that can be configured by -Yreify-copypaste and -Yreify-debug.

Eugene Burmako
  • 13,028
  • 1
  • 46
  • 59
2

There is Macrocosm's desugar which can show how your source code, including, but no limited to macros, is transformed into.

    println("TRANSFORMATION:\n"+ desugar{
        println("a string") 
        MY_MACRO("something")
    })
eruve
  • 661
  • 5
  • 12
  • 1
    I just tried that, but since the expanded macro does not typecheck, it won't typecheck either when I wrap it in a call to desugar. – Kim Stebel Jul 26 '12 at 21:17
  • Sorry, I just noticed that I misunderstood your question indeed. If MY_MACRO fails, there is no way that my example compiles! – eruve Jul 26 '12 at 21:24
  • In that case, so far I have used scala.reflect.makro.FrontEnds#echo() with Eclipse, which is the painful equivalent of debugging a runtime with println()... better than nothing though :) – eruve Jul 26 '12 at 21:32
  • That helps a little bit, but there has to be a way to view the whole generated tree. – Kim Stebel Jul 26 '12 at 21:40
  • Back to the error though, couldn't it be an Apply(Double,...) instead of Apply(Double.apply,...)? – eruve Jul 26 '12 at 21:55
  • No, the error message is talking about the _type_ Double. It does not take type parameters. – Kim Stebel Jul 26 '12 at 22:38