Welcome! Please see the About page for a little more info on how this works.

0 votes
in core.contracts by

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 '(link: 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 (link: x) x)
(defn bar (link: x) x)

;; define 2 contracts
(def c1 (ccc/contract c1-cx "should be odd"

            (link: x) 
            (link: (odd? x))))

(def c2 (ccc/contract c2-cx "should have one digit"

			(link: x)
            (link: (= 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 (link: qux "qux" c1 c2))

;; on the other hand, using with-constraints works as expected.
(def qux-g
(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

1 Answer

0 votes
by
Reference: https://clojure.atlassian.net/browse/CCONTRACTS-4 (reported by alex+import)
...