Trammel is a Clojure providing contracts programming (sometimes called "Design by Contract" or DbC) capabilities. Features of Trammel currently include:
Trammel is inspired by Eiffel and Racket Scheme.
You can use core.cache in your Leiningen and Cake projects with the following :dependencies
directive in your project.clj
file:
[trammel "0.7.0]
For Maven-driven projects, use the following slice of XML in your pom.xml
's <dependencies>
section:
<dependency>
<groupId>org.clojure</groupId>
<artifactId>trammel</artifactId>
<version>0.7.0</version>
</dependency>
Enjoy!
Reference type invariants work in the following ways.
Atoms are created directly with their invariants:
(def a (constrained-atom 0
"only numbers allowed"
[number?]))
@a
;=> 0
And checked on change:
(swap! a inc)
;=> 1
Invariant violations are reported right away:
(swap! a str)
; Pre-condition failure: only numbers allowed
(compare-and-set! a 0 "a")
; Pre-condition failure: only numbers allowed
Refs are created directly with their invariants:
(def r (constrained-ref 0
"only numbers allowed"
[number?]))
And also checked on change, within a transaction:
(dosync (alter r inc))
;=> 1
(dosync (alter r str))
; Pre-condition failure: only numbers allowed
Vars are created directly with their invariants:
(defconstrainedvar ^:dynamic foo 0
"only numbers allowed in Var foo"
[number?])
Var invariants are checked on rebinding, such as with binding
:
(binding [foo :a] [foo])
; Pre-condition failure: only numbers allowed
Agents are created directly with their invariants:
(def ag (constrained-agent 0
"only numbers allowed"
[number?]))
And are checked on send
and send-off
(send ag str)
(agent-error ag)
However, the invariant violations are reported consistently with the agent setup. In this case, the errors are accessible via the agent-error
function.
The following capabilities are under design, development, or consideration for future versions of Trammel:
defconstraint
-- with ability to relax requires and tighten ensuresMore planning is needed around capabilities not listed nor thought of.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close