(as a & [b :as rests])
A better assert. Two arities are possible: [a & [b]]
If assert is false, this is a no-op -- it just passes through the expression-or-value ('a' if there is no 'b', or 'b'). In ClojureScript, just set :elide-asserts to true as a compiler flag in your project.clj to turn off these assertions.
If assert is on and there is no predicate-fn, it will just assert the expression and then return it (unlike normal assert which always returns nil). Useful for making sure your gets and get-ins and keyword functions are actually returning something non-nil, but without having to separately bind then assert them: just wrap them in 'as'.
If there is a predicate it will first assert the (a b) expression, then return just 'b'. If the predicate fails, then the expression, it's evaluated value, and the predicate are all shown in the assertion message.
Examples:
Ensure map entry exists or is not nil (will either return it or throw an assertion failure): (as (get some-map :some-key))
Ensure a value is an int and is odd: (as (every-pred int? odd?) (get some-map :some-number))
Remember, in production code, this is compiled just as (get some-map :some-number) with nothing else wrapping it.
Ensure a value is 35: (as #(= 35 %) some-value-or-expression)
If nil or false are valid possibilities, you can use combinations of 'or', 'nil?' and 'false?' in your predicate function as desired. These are just normal Clojure functions, no magic.
A better assert. Two arities are possible: [a & [b]] 1. (as some-expression-or-value) or 2. (as some-predicate-fn some-expression-or-value) If *assert* is false, this is a no-op -- it just passes through the expression-or-value ('a' if there is no 'b', or 'b'). In ClojureScript, just set :elide-asserts to true as a compiler flag in your project.clj to turn off these assertions. If *assert* is on and there is no predicate-fn, it will just assert the expression and then return it (unlike normal assert which always returns nil). Useful for making sure your gets and get-ins and keyword functions are actually returning something non-nil, but without having to separately bind then assert them: just wrap them in 'as'. If there is a predicate it will first assert the (a b) expression, then return just 'b'. If the predicate fails, then the expression, it's evaluated value, and the predicate are all shown in the assertion message. Examples: Ensure map entry exists or is not nil (will either return it or throw an assertion failure): (as (get some-map :some-key)) Ensure a value is an int and is odd: (as (every-pred int? odd?) (get some-map :some-number)) Remember, in production code, this is compiled just as (get some-map :some-number) with nothing else wrapping it. Ensure a value is 35: (as #(= 35 %) some-value-or-expression) If nil or false are valid possibilities, you can use combinations of 'or', 'nil?' and 'false?' in your predicate function as desired. These are just normal Clojure functions, no magic.
(when-asserts & forms)
Evaluates the forms only when assert is true. Always returns nil. Is a no-op if assert is false -- no code is emmitted. In ClojureScript, just set :elide-asserts to true as a compiler flag in your project.clj to turn off these assertions.
Evaluates the forms only when *assert* is true. Always returns nil. Is a no-op if *assert* is false -- no code is emmitted. In ClojureScript, just set :elide-asserts to true as a compiler flag in your project.clj to turn off these assertions.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close