Disable code depending on optimsation level

I have input and consistency checks that I need during development but want to disable for final runs of my code. Is there a way to annotate these checks s.t. they are not included at higher optimisation levels or depending on other global settings?

For example, if these consistency checks are boundschecks I could annotate the corresponding code with @boundscheck and then after development start a julia process with julia --checkbounds no to disable the corresponding code.

Or in C/C++ I would wrap the corresponding code in a

# ifdef DEBUG
// check consistency
# endif

and have a line # define DEBUG that I would comment out once I am done debugging.

Is there a similar mechanism in julia or what is the recommended way to enable some code only in debugging mode?

See Debug checks without output.

1 Like

You can use @static if, for example to disable code in some version of Julia

@static if VERSION < v"1.6"
    println("I'm old!")
    # (function definition etc. here)

I don’t often see Julia code with distinct debug modes and build modes, though. It’s less useful in a dynamic language where you don’t as often need to rerun your stuff in debug mode to figure out what is happening - you can usually use the REPL to interactively debug your problems much more effectively.