Skip to content

Commit

Permalink
Cleaned up the contract builder and added the contract info string to…
Browse files Browse the repository at this point in the history
… the pre/post error
  • Loading branch information
fogus committed Dec 8, 2010
1 parent 625d1fe commit edd4b9a
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions src/fogus/me/trammel.clj
Expand Up @@ -102,7 +102,7 @@
([f x] {:pre [(foo x)] :post [(bar %)]} (f x)) ([f x] {:pre [(foo x)] :post [(bar %)]} (f x))
" "
[cnstr] [n cnstr]
(let [[args pre-post-map] cnstr] (let [[args pre-post-map] cnstr]
`(~(into '[f] args) `(~(into '[f] args)
(let [ret# (try (let [ret# (try
Expand All @@ -114,13 +114,13 @@
:else [item])) :else [item]))
args)))) args))))
(catch AssertionError pre# (catch AssertionError pre#
(throw (AssertionError. (str "Pre-condition failure! " (.getMessage pre#))))))] (throw (AssertionError. (str "Pre-condition failure in " ~n "! " (.getMessage pre#))))))]
(try (try
((fn [] ((fn []
~(select-keys pre-post-map [:post]) ~(select-keys pre-post-map [:post])
ret#)) ret#))
(catch AssertionError post# (catch AssertionError post#
(throw (AssertionError. (str "Post-condition failure! " (.getMessage post#)))))))))) (throw (AssertionError. (str "Post-condition failure in " ~n "! " (.getMessage post#))))))))))


(defmacro contract (defmacro contract
"The base contract form returning a higher-order function that can then be partially "The base contract form returning a higher-order function that can then be partially
Expand Down Expand Up @@ -155,16 +155,17 @@
If you're so inclined, you can inspect the terms of the contract via its metadata, keyed on If you're so inclined, you can inspect the terms of the contract via its metadata, keyed on
the keyword `:constraints`. the keyword `:constraints`.
" "
[name docstring & constraints] [n docstring & constraints]
(let [raw-cnstr (partition 2 constraints) (let [raw-cnstr (partition 2 constraints)
arity-cnstr (for [[a c] raw-cnstr] arity-cnstr (for [[a c] raw-cnstr]
(build-constraints-map a c)) (build-constraints-map a c))
fn-arities (for [b arity-cnstr] fn-arities (for [b arity-cnstr]
(build-contract b))] (build-contract docstring b))
(list `with-meta body (list* 'fn n fn-arities)]
(list* `fn name fn-arities) `(with-meta
`{:constraints (into {} '~arity-cnstr) ~body
:docstring ~docstring}))) {:constraints (into {} '~arity-cnstr)
:docstring ~docstring})))


(defn with-constraints (defn with-constraints
"A contract combinator. "A contract combinator.
Expand Down Expand Up @@ -309,11 +310,10 @@
(defconstrainedtype Bar [a 4 b 8] [(every? pos? [a b])]) (defconstrainedtype Bar [a 4 b 8] [(every? pos? [a b])])
(Bar? (new-Bar)) (Bar? (new-Bar))


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


(provide-contracts (provide-contracts
[sqr "The constraining of sqr" [n] [number? (not= 0 n) => pos? number?]]) [sqr "the constraining of sqr" [n] [number? (not= 0 n) => pos? number?]])


(sqr 0) (sqr 0)


Expand Down

0 comments on commit edd4b9a

Please sign in to comment.