If an io! block occurs in a transaction, throws an
IllegalStateException, else runs body in an implicit do. If the
first expression in body is a literal string, will use that as the
exception message.
user=> (def a (ref 0))
#'user/a
user=> (dosync
(io! (println "hello"))
(alter a inc))
IllegalStateException I/O in transaction
user=> (dosync
(println "hello")
(alter a inc))
"hello"
1
user=> (defn fn-with-io []
(io! (println "hello")))
#'user/fn-with-io
user=> (dosync
(fn-with-io)
(alter a inc))
IllegalStateException I/O in transaction
(defmacro io!
"If an io! block occurs in a transaction, throws an
IllegalStateException, else runs body in an implicit do. If the
first expression in body is a literal string, will use that as the
exception message."
{:added "1.0"}
[& body]
(let [message (when (string? (first body)) (first body))
body (if message (next body) body)]
`(if (clojure.lang.LockingTransaction/isRunning)
(throw (new IllegalStateException ~(or message "I/O in transaction")))
(do ~@body))))
Comments top
No comments for io!. Log in to add a comment.