Skip to main content

copy-pprint-dispatch

copy-pprint-dispatch Function

Syntax:

copy-pprint-dispatch &optional table → new-table

Arguments and Values:

table—a pprint dispatch table, or nil.

new-table—a fresh pprint dispatch table.

Description:

Creates and returns a copy of the specified table, or of the value of *print-pprint-dispatch* if no table is specified, or of the initial value of *print-pprint-dispatch* if nil is specified.

Exceptional Situations:

Should signal an error of type type-error if table is not a pprint dispatch table.

Expanded Reference: copy-pprint-dispatch

Copying the Current Dispatch Table

With no arguments, copy-pprint-dispatch copies the current value of *print-pprint-dispatch*.

(let ((my-table (copy-pprint-dispatch)))
(not (eq my-table *print-pprint-dispatch*)))
=> T

Getting the Initial (Standard) Dispatch Table

Passing nil returns a copy of the initial dispatch table, which contains the standard pretty-printing rules.

;; Reset to standard pretty-printing by copying the initial table
(let ((*print-pprint-dispatch* (copy-pprint-dispatch nil))
(*print-pretty* t))
(write-to-string '(defun foo (x) (+ x 1))))
=> "(DEFUN FOO (X) (+ X 1))"

Safe Modification Without Affecting Global State

Copying ensures that modifications to the new table do not affect the original.

(let ((*print-pprint-dispatch* (copy-pprint-dispatch))
(*print-pretty* t))
(set-pprint-dispatch 'string
(lambda (stream obj)
(format stream "STR(~A)" obj)))
;; Only affects the local copy
(write-to-string "hello"))
;; => "STR(hello)"

;; The global table is unmodified
(let ((*print-pretty* t))
(write-to-string "hello"))
;; => "\"hello\""

Copying a Specific Table

You can pass an existing dispatch table to copy it.

(let* ((table-a (copy-pprint-dispatch))
(table-b (copy-pprint-dispatch table-a)))
(not (eq table-a table-b)))
=> T