当使用断言时,我遇到了一些烦人的问题
这是一个简单的scala宏示例。
答案 0 :(得分:1)
这是一个解决方案,提供有关断言失败的更深入细节,并在抛出异常时消除内部函数的所有scala层:
def assert2(c: Context)(act: c.Expr[Any],exp: c.Expr[Any]): c.Expr[Unit] = {
import c.universe._
val actm = act.tree.toString
val expm = exp.tree.toString
reify({
if(act.splice!=exp.splice) {
try {
throw new Exception("AssertionError: "+c.Expr[String](Literal(Constant(actm))).splice+"["+act.splice+"]==["+exp.splice+"]"+c.Expr[String](Literal(Constant(expm))).splice)
} catch {
case unknown: Throwable => System.err.println(""+unknown+unknown.getStackTrace.toList.filter(_.toString.indexOf("scala.")!=0).mkString("\n ","\n ","\n ")); exit
}
}
})
}
def myAssert2(act: Any, exp: Any) = macro assert2