Skip to content
Browse files

Added constrained agents and refs

  • Loading branch information...
1 parent fbdb5a6 commit d91ecf85989ef2292c8d3669a47fb5c3c68d426b @fogus fogus committed Mar 30, 2012
Showing with 40 additions and 0 deletions.
  1. +40 −0 src/trammel/core.clj
View
40 src/trammel/core.clj
@@ -312,8 +312,48 @@
(fn [x#] true)))
r#)))
+(defmacro constrained-ref
+ [init inv-description invariants]
+ `(do
+ (let [r# (ref ~init)]
+ (set-validator! r# (partial (contract ~(symbol (str "chk-ref" ))
+ ~inv-description
+ [the-ref#]
+ ~invariants)
+ (fn [x#] true)))
+ r#)))
+
+
+(defmacro constrained-agent
+ [init inv-description invariants]
+ `(do
+ (let [r# (agent ~init)]
+ (set-validator! r# (partial (contract ~(symbol (str "chk-agent" ))
+ ~inv-description
+ [the-agent#]
+ ~invariants)
+ (fn [x#] true)))
+ r#)))
(comment
+ (def ag (constrained-agent 0
+ "only numbers allowed"
+ [number?]))
+
+ (send ag str)
+
+ @ag
+
+ (agent-error ag)
+
+ (def r (constrained-ref 0
+ "only numbers allowed"
+ [number?]))
+
+ (dosync (alter r inc))
+
+ (dosync (alter r str))
+
(def a (constrained-atom 0
"only numbers allowed"
[number?]))

0 comments on commit d91ecf8

Please sign in to comment.
Something went wrong with that request. Please try again.