core.contracts

provide macro fails for multiple constraints

Details

  • Type: Defect Defect
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • Environment:
    clojure 1.5
    core.contracts 0.5

Description

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"
[x]
[(odd? x)]))

(def c2 (ccc/contract c2-cx "should have one digit"
[x]
[(= 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.
(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

Activity

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated: