I am developing a package that should work quickly and be correct. I want to write only one function, but I have two βversionsβ of this function: one that immediately stops when it detects some kind of funny business, and the other as quickly as possible. My idea is to run a strict version of the function on a random selection of inputs and, provided that nothing works, run the fast version on the entire set of inputs. I definitely don't have the processing power to run every test on every input, although I could sleep easier if I could. (I am open to the idea that this is already the wrong approach to the verification problem, so if you have a better organizational approach, feel free to suggest this instead of the specific improvement below).
So far, I have cracked a workaround for this problem as follows:
module Foo
export some_function
const using_removeable_assertions = true
macro RemoveablyAssert(the_condition)
if using_removeable_assertions
return esc(:($the_condition || error("Assertion failed.")))
else
return esc("")
end
end
function some_function(x::Float64,y::Float64)
@RemoveablyAssert x>0
@RemoveablyAssert y>0
(x + y/2.0)::Float64
end
end
using Foo
, ,
julia> some_function(1.0,1.0)
1.5
julia> some_function(1.0,-1.0)
ERROR: Assertion failed.
in some_function at none:18
, const using_removeable_assertions = false, ,
julia> some_function(1.0,1.0)
1.5
julia> some_function(1.0,-1.0)
0.5
, , , :
julia> @code_native some_function(1.0,1.0)
.section __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 19
pushq %rbp
movq %rsp, %rbp
movabsq $13174486592, %rax
Source line: 19
vmulsd (%rax), %xmm1, %xmm1
vaddsd %xmm0, %xmm1, %xmm0
popq %rbp
ret
.
function some_function_2(x::Float64,y::Float64)
(x + y/2.0)::Float64
end
julia> @code_native some_function_2(1.0,1.0)
.section __TEXT,__text,regular,pure_instructions
Filename: none
Source line: 2
pushq %rbp
movq %rsp, %rbp
movabsq $13174493536, %rax
Source line: 2
vmulsd (%rax), %xmm1, %xmm1
vaddsd %xmm0, %xmm1, %xmm0
popq %rbp
ret
: ,
, .
: , . , some_function_safe some_function, , , , - julia, . . using[safemode=yes] Foo.
, , "", , . " ", , . [safemode=yes] , , , , , .
(FWIW, , , . . , , -, , , , , ..)