liveBook · Manning

Io!

As explained in Clojure: The Essential Reference

io! is a Clojure macro that marks code as unsuitable for execution inside a transaction by throwing an exception there.

io! is a Clojure macro for marking code as unsuitable for transaction execution. When that code runs inside dosync, io! throws an exception instead of allowing the operation to continue. section 14.4

Rejecting I/O inside a transaction

f2 calls io! around println and is then passed to commute inside dosync. Calling f1 produces IllegalStateException: I/O in transaction. section 14.4

(def counter (ref 0))

(defn f2 [value] ; #1
  (io! (println "Sorry, side effect on" value))
  (inc value))

(defn f1 [] ; #2
  (dosync (f2 (commute counter f2))))

(f1) ; #3
;; IllegalStateException I/O in transaction  user/f2

Trace io! from the side-effecting function through commute and dosync to the resulting exception. section 14.4

How Clojure: The Essential Reference covers it

How deep it goes
Demonstrates invocation through commute and the resulting exception, without exposing macro expansion or deeper implementation details.
Examples
Uses println inside f2, invokes f2 through commute, and observes IllegalStateException from f1.
What it assumes
Assumes familiarity with dosync, commute, refs, and transaction-oriented concurrency.
What you can do after
Apply it to reject I/O during transaction execution and recognize the resulting IllegalStateException.
How it connects
Connects transaction safety with dosync, commute, refs, and software transactional memory.

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.