Skip to content

Commit b49c198

Browse files
puredangerrichhickey
authored andcommitted
add *print-namespace-maps* flag to control namespace map printing - false by default, but true by default in the repl
Signed-off-by: Rich Hickey <[email protected]>
1 parent de89920 commit b49c198

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

src/clj/clojure/core_print.clj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@
3838

3939
(def ^:dynamic *verbose-defrecords* false)
4040

41+
(def ^:dynamic
42+
^{:doc "*print-namespace-maps* controls whether the printer will print
43+
namespace map literal syntax. It defaults to true."
44+
:added "1.9"}
45+
*print-namespace-maps* false)
46+
4147
(defn- print-sequential [^String begin, print-one, ^String sep, ^String end, sequence, ^Writer w]
4248
(binding [*print-level* (and (not *print-dup*) *print-level* (dec *print-level*))]
4349
(if (and *print-level* (neg? *print-level*))
@@ -240,10 +246,12 @@
240246

241247
(defmethod print-method clojure.lang.IPersistentMap [m, ^Writer w]
242248
(print-meta m w)
243-
(let [[ns lift-map] (lift-ns m)]
244-
(if ns
245-
(print-prefix-map (str "#:" ns) lift-map pr-on w)
246-
(print-map m pr-on w))))
249+
(if *print-namespace-maps*
250+
(let [[ns lift-map] (lift-ns m)]
251+
(if ns
252+
(print-prefix-map (str "#:" ns) lift-map pr-on w)
253+
(print-map m pr-on w)))
254+
(print-map m pr-on w)))
247255

248256
(defmethod print-dup java.util.Map [m, ^Writer w]
249257
(print-ctor m #(print-map (seq %1) print-dup %2) w))

src/clj/clojure/main.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
*print-meta* *print-meta*
7575
*print-length* *print-length*
7676
*print-level* *print-level*
77+
*print-namespace-maps* true
7778
*data-readers* *data-readers*
7879
*default-data-reader-fn* *default-data-reader-fn*
7980
*compile-path* (System/getProperty "clojure.compile.path" "classes")

test/clojure/test_clojure/printer.clj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,6 @@
134134
(ex-info "the root"
135135
{:with "even" :more 'data})))))))
136136

137-
137+
(deftest print-ns-maps
138+
(is (= "#:user{:a 1}" (binding [*print-namespace-maps* true] (pr-str {:user/a 1}))))
139+
(is (= "{:user/a 1}" (binding [*print-namespace-maps* false] (pr-str {:user/a 1})))))

0 commit comments

Comments
 (0)