Is there a feature like C's #define in order to compile or not e.g. println

Hi,

I would like to activate/deactivate integration of some println in my compiled code.
I guess I could set a debug constant and fill the code with:

const debug = 0

if debug == 1
    println("value = " * value)
end

I suppose that it won’t be compiled in (am I right ?).

But maybe there’s some Julia’s way to do that ?

A better way to do this would be with a function, e.g.

debug() = false

function foo(value)
    if debug()
        println("value = ", value)
    end
end

then

julia> code_llvm(foo, Tuple{Int})
; Function Signature: foo(Int64)
;  @ REPL[26]:1 within `foo`
define void @julia_foo_18366(i64 signext %"value::Int64") #0 {
top:
  ret void
}

and if you change debug(), then any functions which depend on it will be automatically recompiled when they’re called again:

julia> debug() = true;

julia> code_llvm(foo, Tuple{Int})
; Function Signature: foo(Int64)
;  @ REPL[26]:1 within `foo`
define void @julia_foo_18399(i64 signext %"value::Int64") #0 {
top:
;  @ REPL[26]:3 within `foo`
  call void @j_println_18402(ptr nonnull @"jl_global#18403.jit", i64 signext %"value::Int64")
  ret void
}

If you are interested in debug logs (i.e., it’s not just a toy example that you used as a substitute for another, more complex problem), then there is a @debug macro that can be controlled by an environment variable: Logging · The Julia Language

Copy-pasting an example from the referenced documentation:

julia> foo() = @debug "foo"
foo (generic function with 1 method)

julia> foo()

julia> ENV["JULIA_DEBUG"] = Main
Main

julia> foo()
┌ Debug: foo
└ @ Main REPL[1]:1

The closest analogue of C’s #ifdef in Julia is probably @static if. But you rarely need this — usually relying on the compiler for constant propagation suffices, and there are other ways to enable debugging logs as noted above.

Note that this doesn’t do what the OP wanted though. The @debug code is still in the compiled function whether or now you have ENV["JULIA_DEBUG"] = Main, it’s just not displaying.