liveBook · Manning

Clojure.repl

As explained in Clojure: The Essential Reference

clojure.repl is the Clojure namespace containing utilities for documentation lookup, source inspection, namespace browsing, and exception reporting in the REPL.

clojure.repl provides tools for answering questions while working at a Clojure REPL: What does this var do? What names are available? What caused this exception? Common helpers including doc, source, apropos, dir, and pst are available directly at the REPL without an explicit require. section 22.3

Find documentation and definitions:

  • Use (doc name) when you know the symbol. It prints documentation from a var’s or namespace’s metadata; a var without a :doc entry shows nil. section 22.3
  • Use (find-doc "^to-") to search documentation with a regular expression. A broad pattern may produce pages of matches because it searches names and documentation strings. Use (apropos "to-") when only matching names matter. section 22.3
  • Use (dir clojure.reflect) to print a namespace’s public definitions in order. Use (dir-fn 'clojure.reflect) when you need those names as a sequence to process instead. section 22.3

Read an implementation

(source unchecked-inc-int) prints its definition. For source text you can manipulate, (source-fn 'not-empty) returns a string rather than printing the definition. section 22.3

For a REPL exception, *e holds the most recent exception and (pst) prints its stack trace. Pass an exception to pst explicitly, or add a frame limit, as in (pst ex 4). Chained exceptions appear under “Caused by”; (root-cause ex) selects the underlying cause. For lower-level trace formatting, demunge makes generated names readable and stack-element-str formats a stack element. section 22.3

(def ex ; #1
  (ex-info "Problem." {:status :surprise}
    (try (/ 1 0)
    (catch Exception e
      (ex-info "What happened?" {:status :unkown} e)))))

(pst ex 3) ; #2

;; ExceptionInfo Problem. {:status :surprise}
;;         clojure.core/ex-info (core.clj:4617)
;;         clojure.core/ex-info (core.clj:4617)
;;         clojure.lang.Compiler$InvokeExpr.eval (Compiler.java:3652)
;; Caused by:
;; ExceptionInfo What happened? {:status :unkown}
;;         clojure.core/ex-info (core.clj:4617)
;;         clojure.core/ex-info (core.clj:4617)
;;         user/fn--2169 (form-init4179141376169992155.clj:5)
;; Caused by:
;; ArithmeticException Divide by zero
;;         clojure.lang.Numbers.divide (Numbers.java:158)
;;         clojure.lang.Numbers.divide (Numbers.java:3808)

A nested exception demonstrates how pst displays each cause in a chain, even when its frame count is limited. section 22.3

How Clojure: The Essential Reference covers it

How deep it goes
Works through individual helpers, including printed versus returned results and chained exceptions, without explaining REPL implementation internals.
Examples
Uses documentation metadata, name searches, namespace listings, source definitions, division by zero, and nested exceptions.
What it assumes
Assumes basic use of the Clojure REPL, symbols, and namespaces while illustrating metadata and exception inspection with examples.
What you can do after
Apply it via clojure.repl to locate definitions and documentation, read source, and diagnose exception causes.
How it connects
Connects REPL evaluation with namespace browsing, var metadata, source inspection, and exception stack traces.

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.