Switch off chunks of code

I’m wondering if there’s a way to completely remove sections of debug/analytics code via some switch, similar to #define in C. Currently I’m just doing this.

function f(x, analyse=false)
    # compute output
    if analyse
        # compute rarely-needed expensive statistics
    end
end

This is OK, but it would be nice to tell the compiler to just ignore this code when compiling for production. I believe the answer is metaprogramming, which can go deeper than I’m willing to dive, but I suspect the solution here is relatively simple.

I found a couple of related threads on stack overflow etc., but nothing that quite matches.

1 Like

Would @debug work for your use case? Logging · The Julia Language

3 Likes

FYI, @debug doesn’t seem to construct the output if the enabled loglevel is higher than the one specified by the macro (Debug in this case). So it should work properly.

2 Likes

Yes it would!. Thanks.