我的 Julia 代码调用断言函数(或宏,我不确定)。 在 Julia v0.1.2 下运行时,触发断言时,它会评估第二个参数(字符串)并正确打印评估的实例。在下面的示例中,v0.1.2 将打印“Hello world”。
升级到 v0.2.1 后,第二个参数不会被计算。相反,正在打印一条不清楚的消息骨架:
julia> a="world" ; @assert(1==0,"hello $a")
ERROR: assertion failed: :($(Expr(:string, "hello ", :a)))
in error at error.jl:21
julia> a="world" ; assert(1==0,"hello $a")
ERROR: assertion failed: y
in assert at deprecated.jl:21
有人能解释一下这种新行为吗? 有没有办法使用内置断言恢复旧行为,或者我最好实现自己的断言函数?
最佳答案
宏现在在编译时对断言文本进行字符串化,主要是出于性能方面的考虑。您当然可以添加并使用自己的宏:
julia> macro assert2(ex, text)
:($ex ? nothing : error("Assertion failed: ", $(text)))
end
julia> @assert2(1==2, "hello $a")
ERROR: Assertion failed: hello abc
in error at error.jl:22
关于assert - Julia 的 @assert 不再评估断言消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23167868/