liveBook · Manning

Read

As explained in Clojure: The Essential Reference

The :read function is a clojure.main/repl callback that obtains and parses the next input expression.

read transforms textual Clojure syntax from an input stream into a data structure, which may represent either data or executable code. In a REPL, it obtains the next form before eval processes it; clojure.main/repl can replace this reader to support a different input language. section 17.0 section 22.4

The reader’s practical contract includes:

  • Accept a java.io.PushbackReader or subclass; calling read without an explicit stream reads from *in*. section 17.1
  • Return the next parsed form, while supporting :eof to choose the result when no form remains. section 17.1
  • Control reader conditionals with :read-cond set to :allow or :preserve, and select a platform with :features. section 17.1
  • Treat #= specially according to *read-eval*; bind it to false to reject evaluation or :unknown to reject reading. section 17.1

Replacing the REPL reader

A custom calculator REPL supplies :read with a function receiving request-prompt and request-exit. It skips whitespace at line boundaries, reads a line, and returns a regex match such as ["2*3" "2" "*" "3"]. A matching :eval function converts the operands and applies the selected operator, producing 6. section 22.4

How Clojure: The Essential Reference covers it

How deep it goes
Explains streams, options, EOF, conditional forms, and reader evaluation controls, then demonstrates a custom callback without detailing reader internals.
Examples
Uses *in*, PushbackReader inputs, reader conditionals, EOF handling, #= evaluation, and an infix calculator REPL.
What it assumes
Assumes familiarity with Clojure forms, streams, REPL evaluation, and dynamic vars while demonstrating basic input parsing.
What you can do after
Operate/configure a REPL reader, parse Clojure forms from streams, and constrain conditional, EOF, and read-time evaluation behavior.
How it connects
Ties read to the REPL, eval, read-string, reader conditionals, tagged literals, and EDN.

Related concepts

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.