作为一个老 C 程序员,我在我的代码中使用了很多断言。现在我想全局关闭它们以加快速度。这样做的最佳实践方法是什么?
最佳答案
没有内置选项/命令行标志来禁用 @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
(取自 https://discourse.julialang.org/t/assert-alternatives/24775/14)
关于julia - 如何在 Julia 中禁用 @assert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58305613/