Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 62 lines (43 sloc) 1.36 kb

core.contracts

First add the following to your Leiningen project.clj file in the :dependencies section:

[org.clojure/core.contracts "0.0.1"]

To include core.contracts, add it to your namespace declaration:

(ns my.awesome.lib
  (require 
    [clojure.core.contracts :as contracts]))

Write your functions as you normally would:

(defn sqr [n] (* n n))

(sqr 10)
;=> 100

(sqr 0)
;=> 0

Later, annotate your functions separately with the constraints:

(contracts/provide 
  (sqr "Constraints for squaring" 
    [x] [number? (not= 0 x) => number? pos?]))

The core.contracts validation will occur at run-time:

(sqr 10)
;=> 100

(sqr 0)
; java.lang.AssertionError: 
;   Pre-condition failed: (not= 0 x)

To turn-off contracts for any given file, add the following to the top of the file in question before compiling:

(set! *assert* false)
Something went wrong with that request. Please try again.