ANN: ArgCheck.jl

ArgCheck.jl is a very small package that allows to replace

function f(A, B, k, n)
    (k > 0) && (n > 0) || throw(ArgumentError("k and n must be positive".))
    k > n || throw(ArgumentError("k > n must hold."))
    size(A) == size(B) || throw(DimensionMismatch("size(A) == size(B) must hold."))
    # doit    
end

by

function f(A, B, k, n)
    @argcheck k > 0 && n > 0
    @argcheck k > n
    @argcheck size(A) == size(B) DimensionMismatch
    # doit
end
21 Likes

Simple and useful! Good work.

Nice. For reference, there’s been a discussion about including a similar feature in Julia itself, possibly with support for conditions on the returned value too:
https://github.com/JuliaLang/julia/pull/15495

1 Like

Interesting. I think it would be good to have some standard macro for argument checking in Base.

Looks useful. Will you register it?

Nice!

I’ve been looking for this “design by contract” concept from Eiffel language (its only(?) major contribution) before.

I had a feeling macros would work, so didn’t complain it wasn’t built-in to the language.

In theory (and your implmentation, shouldn’t preclude) this could be an optimization opportunity for the compiler. Maybe it already helps it…?

The full idea is also invariants within loops and after loops. Then “argcheck” isn’t the best name…

It is registered maybe you need to run Pkg.update()?

1 Like

@jw3126 I think @assert is well suited to check for invariants before/after loops.

Had to run Pkg.update(), now I installed it. Thanks!