0

I’ve come across some annoying problems when using asserts where there is either

  • no good information available: why did it fails, and what the assert was about
  • the assert information is buried under a sea of unneeded information like too many layers of scala functions. I’ve kept these issues in the back of my head and, now that new scala macros are available , I plan to go on Stack Overflow and create a high-value question and answer pair for each issue. That way, the next person who has the problem won’t have to slog through so much misinformation. I might even learn some more about the various issues that plagued us if other experts chime in with their own knowledge.

This is a simple scala macro example.

Eric Mariacher
  • 341
  • 1
  • 8

1 Answers1

1

Here is a solution giving more in depths details about assert failure and eliminating all scala layers of internal functions when throwing the exception:

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
Eric Mariacher
  • 341
  • 1
  • 8