简体   繁体   中英

Catch Scala macro expansion errors

Is there an easy way to catch errors thrown during macro expansion?

Suppose I have the following code. When I invoke it, I would like to be able to catch and inspect the RuntimeException thrown by fooMacro .

def foo: Unit = macro fooMacro

def fooMacro(c: scala.reflect.macros.whitebox.Context) = {
    throw new RuntimeException("bar") 
}

You might hope that you could supply a Reporter that would receive the exception, but actually it's stringified in the cake:

https://github.com/scala/scala/blob/v2.12.0-M5/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala#L840

There's also -Ymacro-debug-verbose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM