How do I print an expanded macro in Scala?

耗尽温柔 提交于 2019-12-18 18:51:11

问题


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?


回答1:


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.




回答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")
    })


来源:https://stackoverflow.com/questions/11677609/how-do-i-print-an-expanded-macro-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!