Scalaxy/Debug: macros that make Predef.assert, require and assume a joy to use!

Quick link: jump to Scalaxy/Debug straight away!

Throughout languages, assertions have proved to be a handy tool for programmers who want “light” error checks for very unlikely error cases (these checks are typically only performed in Debug builds so as not to slow down Release code).

In C / C++, assert macros usually print out the exact source code of the expression that failed the assertion check, which is often more than enough to understand the meaning of the failure.

In Java and Scala though, you have to provide your own message if you don’t want to be presented with an anonymous failure trace:

How often do you find yourself writing the following code?

Or, more recently (string interpolation FTW!):

Well, thanks to Scalaxy/Debug macros, you can now forget about this kind of grungy code:

You just need to add the following to your build.sbt file to get going:

Oh, and this works with assert, assume and require.

Enjoy! (please file any bug on Scalaxy/Debug’s GitHub and follow me on Twitter if you find this useful :-))

This entry was posted in Uncategorized. Bookmark the permalink.