2
votes

My Julia code invokes the assert function (or macro, I'm not sure). Running under Julia v0.1.2 when the assert is triggered, it evaluated the 2nd argument (string) and printed correctly the evaluated instance. In the example below, v0.1.2 would printing "Hello world".

After upgrading to v0.2.1, the 2nd argument does not get evaluated. Instead an unclear skeleton of the message is being printed:

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

Can anyone shed some light on this new behavior? Is there a way to get back the old behavior with the builtin assert, or will I be better off implementing my own assert function?

1

1 Answers

2
votes

The macro now stringifies the assert text at compile time, mainly because of performance considerations. You can of course add and use your own macro instead:

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