Skip to content
Joshua Mendoza edited this page Jul 6, 2014 · 3 revisions

provide

While defining contracts local to the constrained function is nice (see defconstrainedfn for more information), very often you will find yourself in possession of an existing function that is not constrained:

(sqr 0)
;=> 0
In this case, core.contracts provides the **provide** macro to define contracts and apply them dynamically to existing functions:
(provide
  [sqr "Constraints for squaring"
    [x] [number? (not= 0 x) => number? pos?]])

(sqr 0)
; java.lang.AssertionError:
; Assert failed: (not= 0 n)
As shown, the sqr function is dynamically modified with the contract defined in the body of **provide**. This macro can take any number of vectors where each corresponds to a contract for a given function; including multiple arities embedded within. core.contracts also allows you to include existing named contracts instead of the contract specification vector, as shown below:
(def sqr-contract
  (contract sqr-constraint
    "Defines the constraints on squaring."
    [n]   [number? (not= 0 n)   => pos? number?])

(sqr 0)
;=> 0

(provide-contracts
  [sqr "Apply the contract for squaring"
       sqr-contract])

(sqr 0)
; java.lang.AssertionError:
; Assert failed: (not= 0 n)
**provide** gives you a lot of flexibilty in how to separate functions from their contracts and likewise apply them in domain-specific ways.

Return to documentation

Clone this wiki locally