No matching definitions.

tur/contract

stdlib/contract.tur

runtime contract macros (assert!, require!, ensure!, ...).

Since: Phase C1

defn

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

defmacro

assert!

(assert! [condition])

unconditional sanity check, panics with "Assertion failed" if false.

conditionthe boolean expression to check

nil on success; panics on failure.

(assert! (= x 1))  ; => panics if x != 1

Since: Phase C1

defmacro

assert-msg!

(assert-msg! [condition msg])

unconditional sanity check with custom panic message.

conditionthe boolean expression to check
msgthe 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

defmacro

require!

(require! [condition])

precondition check at function entry, panics with "Precondition failed" if false.

conditionthe boolean precondition to enforce

nil on success; panics on failure.

(require! (> n 0))  ; => panics if n <= 0

Since: Phase C1

defmacro

require-msg!

(require-msg! [condition msg])

precondition check with custom panic message.

conditionthe boolean precondition to enforce
msgthe 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

defmacro

ensure!

(ensure! [condition])

postcondition check, panics with "Postcondition failed" if false.

conditionthe boolean postcondition to enforce

nil on success; panics on failure.

(ensure! (>= result 0))  ; => panics if result is negative

Since: Phase C1

defmacro

ensure-msg!

(ensure-msg! [condition msg])

postcondition check with custom panic message.

conditionthe boolean postcondition to enforce
msgthe 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

defmacro

invariant!

(invariant! [obj predicate])

check a structural invariant on a value, panics with "Invariant failed" if false.

objthe value to check
predicatea 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

defmacro

invariant-msg!

(invariant-msg! [obj predicate msg])

check a structural invariant with custom panic message.

objthe value to check
predicatea single-argument function that returns true if the invariant holds
msgthe 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

defn

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

defn

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.