pprint-dispatch
pprint-dispatch Function
Syntax:
pprint-dispatch object &optional table → function, found-p
Arguments and Values:
object—an object.
table—a pprint dispatch table, or nil. The default is the value of *print-pprint-dispatch*. function—a function designator .
found-p—a generalized boolean.
Description:
Retrieves the highest priority function in table that is associated with a type specifier that matches object. The function is chosen by finding all of the type specifiers in table that match the object and selecting the highest priority function associated with any of these type specifiers. If there is more than one highest priority function, an arbitrary choice is made. If no type specifiers match the object, a function is returned that prints object using print-object.
The secondary value, found-p, is true if a matching type specifier was found in table, or false otherwise.
If table is nil, retrieval is done in the initial pprint dispatch table.
Affected By:
The state of the table.
Exceptional Situations:
Should signal an error of type type-error if table is neither a pprint-dispatch-table nor nil.
Notes:
(let ((*print-pretty* t))
(write object :stream s))
≡ (funcall (pprint-dispatch object) s object)
Expanded Reference: pprint-dispatch
Looking Up a Dispatch Function
pprint-dispatch retrieves the highest-priority pretty-printing function for a given object from a dispatch table. It returns two values: the function and a boolean indicating whether a matching entry was found.
;; Look up the dispatch function for an integer
(multiple-value-bind (fn found-p)
(pprint-dispatch 42)
(list (functionp fn) found-p))
=> (T NIL)
Checking for Custom Entries
The second return value indicates whether a matching type specifier was found in the table.
(let ((*print-pprint-dispatch* (copy-pprint-dispatch)))
(set-pprint-dispatch 'string
(lambda (s obj) (format s "~A" obj)))
(multiple-value-bind (fn found-p)
(pprint-dispatch "hello")
found-p))
=> T
Using the Initial Table
Passing nil as the table argument looks up in the initial (standard) pprint dispatch table.
(multiple-value-bind (fn found-p)
(pprint-dispatch '(defun foo () nil) nil)
found-p)
=> T
Equivalence to Pretty Printing
Calling the returned function on a stream and object is equivalent to pretty-printing the object.
;; These are conceptually equivalent:
;; (let ((*print-pretty* t)) (write object :stream s))
;; (funcall (pprint-dispatch object) s object)
(let ((obj '(+ 1 2)))
(multiple-value-bind (fn found-p)
(pprint-dispatch obj)
(with-output-to-string (s)
(funcall fn s obj))))
=> "(+ 1 2)"