.InvariantsCore
invariant
function
defined in module
InvariantsCore
invariant(fn, title; kwargs...)
Create an invariant with name
title
that checks an input against
fn
which returns either
nothing
when the invariant is satisfied, or an error message when the invariant is violated.
invariant(inv; title=title(inv), kwargs...)
Wrap an invariant
inv
, replacing some of its attributes.
invariant(title, invariants, all; kwargs...)
invariant(title, invariants, any; kwargs...)
Combine multiple invariants
invs
logically. The third argument defines how they are combined. If it is
all
, the resulting invariant requires all
invariants
to pass, while a value of
any
results in an invariant where only one of the
invariants
has to be satisfied.
Every method additionally accepts the following keyword arguments:
description::String = ""
: A description of the invariant that gives explanation and points to related resources.
inputfn = identity
: A function that is applied to an input before checking. Useful when composing multiple invariants.
Basic usage:
using
Invariants
inv
=
invariant
(
"
Is negative
"
)
do
n
n
<
0
?
nothing
:
"
`n` is not negative!
"
end
Invariant("Is negative")
Successful check:
check
(
inv
,
-1
)
✔ Invariant satisfied: Is negative
Failing check:
check
(
inv
,
1
)
⨯ Invariant not satisfied: Is negative
`n` is not negative!
Or just get a Bool:
Throw an error when an invariant is not satisfied:
check_throw
(
inv
,
1
)
LoadError("string", 1, InvariantsCore.InvariantException{InvariantsCore.Invariant, String}(Invariant("Is negative")
, "`n` is not negative!"))
There are
6
methods for invariant
:
invariant(inv::InvariantsCore.Invariant; title, inputfn, description, format)
invariant(inv::InvariantsCore.AbstractInvariant; title, inputfn, description, format)
invariant(title, invariants::AbstractVector{<:InvariantsCore.AbstractInvariant}; kwargs...)
invariant(title, invariants::AbstractVector{<:InvariantsCore.AbstractInvariant}, ::typeof(all; kwargs...)
invariant(title, invariants::AbstractVector{<:InvariantsCore.AbstractInvariant}, ::typeof(any; kwargs...)
invariant(fn, title::String; kwargs...)
The following pages link back here:
Invariants.jl , invariants/hasmethod.jl , invariants/hastype.jl , compose.jl , highlevel.jl