Le jeudi 21 avril 2016 à 22:11 -0700, Robert DJ a écrit :
> I've become fond of using the @assert macro for checking input to
> functions, e.g.
> 
> @assert size(A) == size(B)
> @assert x > 0
> 
> Now I've read discussions like https://github.com/JuliaLang/julia/iss
> ues/10614 where I see that this not recommended and that @assert is
> slow.
> What is then the "right" Julian way that isn't slowing things down?
> Something like
> 
> size(A) != size(B) && throw(ArgumentError())
> x <= 0 && throw(DomainError())
This is the recommended solution, which is not faster but has the
advantage of raising specific exceptions which the caller can check.
Though currently you need to write the error message by hand, which in
the two examples you shown could be done automatically (hence the idea
of @require).

OTOH, @assert should be reserved for actual bugs which are never
supposed to happen (e.g. inconsistent internal state), not for
incorrect arguments.


Regards

Reply via email to