如何在Julia中禁用@assert

opu*_*111 6 assert julia

作为老的C程序员,我在代码中使用了很多断言。现在,我想全局关闭它们,以加快处理速度。最佳做法是什么?

crs*_*nbr 10

没有内置选项/命令行标志来@assert全局禁用s,但(!)。

现在,您可以定义一个@myassert宏,根据全局开关,它是无操作或常规的@assert

asserting() = false # when set to true, this will enable all `@myassert`s

macro mayassert(test)
  esc(:(if $(@__MODULE__).asserting()
    @assert($test)
   end))
end

f(x) = @mayassert x < 2 
Run Code Online (Sandbox Code Playgroud)

(摘自https://discourse.julialang.org/t/assert-alternatives/24775/14