How to run a Julia script in "release-mode"?

e.g I have several Julia functions with a custom check

function foo(x)
   check_greater_than(0, x)
   # do something
end

What is the canonical way to remove the check globally? I know one way to do it, is to define the check function as

function check_greater_than(b, x)
   if !haskey(ENV, "RELEASE")
      x > b || error("not greater than $b")
   end
   true
end

Is there any compiler option like --release=true? or will there be such a compiler option? And is it possible to use --check-bounds=no?

Ok, I find it…

@macroexpand @boundscheck check_function(x)

quote
  if Expr(:boundscheck)
      check_function(x)
  end
end

Err… will it be better to have a similar release option for the compiler? So things like @assert can be removed if someone want to run as a release version.

Ref https://github.com/JuliaLang/julia/pull/25576.

Note that @assert is documented that it might be optimized away in future Julias at some optimization level.

2 Likes