liveBook · Manning

Clojure.pprint/simple-dispatch

As explained in Clojure: The Essential Reference

simple-dispatch is the default Clojure pretty-print dispatch multimethod for selecting formatters based on object types.

clojure.pprint/simple-dispatch is the default multimethod used by clojure.pprint to choose formatting behavior from an object's type. It targets human-readable Clojure data structures, including common maps, vectors, sets, sequences, queues, vars, and dereferenced values. section 23.20 section 18.3

The dispatch table includes methods for:

  • Persistent maps, vectors, sets, queues, sequences, vars, dereferenceable objects, nil, and a default method. section 23.20
  • Generic data rather than Clojure source code; code-shaped lists require the separate code-dispatch strategy for idiomatic indentation. section 18.3

Inspecting and replacing the default dispatch

(require '[clojure.pprint :as pprint]) (pprint/pprint (keys (methods pprint/simple-dispatch))) ;; (nil clojure.lang.PersistentQueue clojure.lang.ISeq :default ;; clojure.lang.IPersistentVector clojure.lang.IPersistentMap ;; clojure.lang.IDeref clojure.lang.IPersistentSet clojure.lang.Var) (def form '(condp = x 1 "one" 2 "two" :else :none)) (pprint/pprint form) ;; (condp ;; = ;; x ;; 1 ;; "one" ;; 2 ;; "two" ;; :else ;; :none) (binding [pprint/*print-pprint-dispatch* pprint/code-dispatch] (pprint/pprint form)) ;; (condp = x ;; 1 "one" ;; 2 "two" ;; :else :none) section 23.20 section 18.3

Table 23.1. The dynamic variables related to pretty printing

Name

Namespace

Default

Description

*print-readably*

clojure.core

true

Print escape sequences literally.

*print-dup*

clojure.core

false

Print objects so they can be read back into memory.

*print-length*

clojure.core

nil

Number of items to print in a collection.

*print-level*

clojure.core

nil

Number of nesting collections level to print.

*print-meta*

clojure.core

false

Should metadata be printed as part of the object.

*print-pretty*

clojure.pprint

true

Enable/disable pretty printing.

*print-pprint-dispatch*

clojure.pprint

fn

Set pretty printer dispatch function.

*print-right-margin*

clojure.pprint

72

Number of columns to use before breaking into a newline.

*print-miser-width*

clojure.pprint

40

The column at which to enter miser style.

*print-suppress-namespaces*

clojure.pprint

nil

Remove namespace qualifier from symbols.

*print-radix*

clojure.pprint

nil

Print radix used for integers or ratio.

*print-base*

clojure.pprint

10

Change base for printing integers or ratio.

Use the pretty-printing dynamic-variable table to connect dispatch selection with output controls such as pretty printing, line width, nesting level, and collection length. section 23.20

How Clojure: The Essential Reference covers it

How deep it goes
Explains the default multimethod and its installed type methods, then contrasts it with code-dispatch without detailing method implementations.
Examples
Uses dispatch-method inspection, a condp form, and switching from simple-dispatch to code-dispatch.
What it assumes
Assumes familiarity with Clojure data structures, pprint, multimethod dispatch, and dynamic variable binding.
What you can do after
Apply it through pprint while selecting code-dispatch when formatting Clojure source rather than generic data.
How it connects
Connects pretty printing with multimethods, code-dispatch, with-pprint-dispatch, and dynamic printer variables.

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.