ClojureDocs

导航

命名空间

断言

  • (断言规格 x)
spec-checking assert expression. Returns x if x is valid? according
to spec, else throws an ex-info with explain-data plus ::failure of
:assertion-failed.

Can be disabled at either compile time or runtime:

If *compile-asserts* is false at compile time, compiles to x. Defaults
to value of 'clojure.spec.compile-asserts' system property, or true if
not set.

If (check-asserts?) is false at runtime, always returns x. Defaults to
value of 'clojure.spec.check-asserts' system property, or false if not
set. You can toggle check-asserts? with (check-asserts bool).
1 Example
; Notice that if you try to execute assert it will always return the given value:

(require ['clojure.spec.alpha :as 's])

(s/assert even? 1)
=> 1

; This is because asserts are turned off by default:
(s/check-asserts?)
=> false

; To enable asserts, run this
(s/check-asserts true)
=> true

; Now your asserts will properly fail
(s/assert even? 3)
; ExceptionInfo Spec assertion failed...

```
See Also

Evaluates expr and throws an exception if it does not evaluate to logical true.

Added by witek

Enable or disable spec asserts that have been compiled with '*compile-asserts*' true. See 'assert'....

Added by abrooks

If true, compiler will enable spec asserts, which are then subject to runtime control via check-asse...

Added by abrooks

Returns the value set by check-asserts.

Added by abrooks
0 Notes
No notes for assert