tur/contract
runtime contract macros (assert!, require!, ensure!, ...).
Since: Phase C1
contract-enabled?
(contract-enabled? :bool)
returns true when contracts are enabled at runtime.
:bool -- always true in v1. Phase C2 will add compile-time stripping controlled by --no-contracts / --release flags.
(contract-enabled?) ; => true
Since: Phase C1
assert!
(assert! [condition])
unconditional sanity check, panics with "Assertion failed" if false.
| condition | the boolean expression to check |
nil on success; panics on failure.
(assert! (= x 1)) ; => panics if x != 1
Since: Phase C1
assert-msg!
(assert-msg! [condition msg])
unconditional sanity check with custom panic message.
| condition | the boolean expression to check | |
| msg | the message string to use when panicking |
nil on success; panics with msg on failure.
(assert-msg! (= x 1) "x must be 1") ; => panics with message if x != 1
Since: Phase C1
require!
(require! [condition])
precondition check at function entry, panics with "Precondition failed" if false.
| condition | the boolean precondition to enforce |
nil on success; panics on failure.
(require! (> n 0)) ; => panics if n <= 0
Since: Phase C1
require-msg!
(require-msg! [condition msg])
precondition check with custom panic message.
| condition | the boolean precondition to enforce | |
| msg | the message string to use when panicking |
nil on success; panics with msg on failure.
(require-msg! (> n 0) "n must be positive") ; => panics with message if n <= 0
Since: Phase C1
ensure!
(ensure! [condition])
postcondition check, panics with "Postcondition failed" if false.
| condition | the boolean postcondition to enforce |
nil on success; panics on failure.
(ensure! (>= result 0)) ; => panics if result is negative
Since: Phase C1
ensure-msg!
(ensure-msg! [condition msg])
postcondition check with custom panic message.
| condition | the boolean postcondition to enforce | |
| msg | the message string to use when panicking |
nil on success; panics with msg on failure.
(ensure-msg! (>= result 0) "result must be non-negative")
Since: Phase C1
invariant!
(invariant! [obj predicate])
check a structural invariant on a value, panics with "Invariant failed" if false.
| obj | the value to check | |
| predicate | a single-argument function that returns true if the invariant holds |
nil on success; panics on failure.
(invariant! my-list non-empty?) ; => panics if my-list fails non-empty?
Since: Phase C1
invariant-msg!
(invariant-msg! [obj predicate msg])
check a structural invariant with custom panic message.
| obj | the value to check | |
| predicate | a single-argument function that returns true if the invariant holds | |
| msg | the message string to use when panicking |
nil on success; panics with msg on failure.
(invariant-msg! my-list non-empty? "list must not be empty")
Since: Phase C1
set-contract-handler!
(set-contract-handler! [handler :int] :void)
install a global contract-violation handler.
| handler :int -- a function pointer (as :int) called on contract failure | ||
| with signature (fn [msg :cstr] :void) |
:void
(set-contract-handler! my-handler)
Since: Phase CT4
with-contract-handler
(with-contract-handler [h :int body :int] :int)
execute body with a temporary contract handler.
| h :int -- handler function pointer (fn [msg :cstr] :void), as :int | ||
| body :int -- thunk to run (fn [] :int), as :int |
:int -- the return value of body (as :int)
(with-contract-handler my-handler (fn [] (check-something)))
Since: Phase CT4
Internal definitions
tur-contract-check-- internal helper called by contract macros.tur-contract-check-inv-- internal helper for invariant macros.