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
