Affects Version/s: None
Fix Version/s: None
The following code shows 2 ways for defining a function with constraints.
=> it works as expected using the with-constraints macro
=> it fails on the second contract using the provide macro
(require '[clojure.core.contracts :as ccc])
;; because the provide macro alters the var-root, let's keep two separate identical functions for the test.
(defn qux [x] x)
(defn bar [x] x)
;; define 2 contracts
(def c1 (ccc/contract c1-cx "should be odd"
(def c2 (ccc/contract c2-cx "should have one digit"
[(= 1 (count (str x)))]))
;; using the provide macro => c1 is asserted, c2 never. When we swap around c2 c1, then c2 is asserted, c1 never
(ccc/provide [qux "qux" c1 c2])
;; on the other hand, using with-constraints works as expected.
(ccc/with-constraints bar c1 c2))
(qux 2) ;; expected assertion "should be odd"
(qux 3) ;; expected 3
(qux 23) ;; expected assertion "only one digit", but we get 23
(qux-g 2) ;; should be odd
(qux-g 3) ;; OK
(qux-g 23) ;; should be positive