clojure - Suppress namespace when printing ::keyword -
when write
(println '(:keyword1 :keyword2))
i following output:
(:keyword1 :keyword2)
and it's ok.
but when write
(println '(::keyword1 ::keyword2))
i this
(:namespace/keyword1 :namespace/keyword2)
and want
(::keyword1 ::keyword2)
is possible? make symbolic computations lots of keywords , need print them.
i tried this: https://clojuredocs.org/clojure.pprint/*print-suppress-namespaces* did not help.
thank advise.
the printing machinery in clojure based on multimethods. possible take advantage of in order change how namespaced keywords printed. altering way keywords printed may not wise in general though.
the multimethod used produce output human consumption called print-method. takes 2 arguments - object print , output writer. dispatch performed on type of object being printed, speaking. possible re-define print-method
clojure.lang.keyword
type. default implementation of method can used reference.
it necessary find out if keyword has namespace component - how can determine if should print 1 colon or two. getnamespace
method in keyword class job. @ point way deep clojure implementation details. proceed @ own risk.
(import 'clojure.lang.keyword) ;=> clojure.lang.keyword (import 'java.io.writer) ;=> java.io.writer (defmethod print-method keyword [^keyword k, ^writer w] (if (.getnamespace k) (.write w (str "::" (name k))) (.write w (str k)))) ;=> #object[clojure.lang.multifn 0x24243e9f "clojure.lang.multifn@24243e9f"] (println ::foo) ;=> ::foo ;=> nil
Comments
Post a Comment