<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Questions without answers in Printing</title>
<link>https://ask.clojure.org/index.php/unanswered/clojure/printing</link>
<description></description>
<item>
<title>print-method is inconsistent with regard to clojure.pprint with *print-level*</title>
<link>https://ask.clojure.org/index.php/13337/print-method-inconsistent-regard-clojure-pprint-print-level</link>
<description>&lt;p&gt;Repro:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;;; print-method

user=&amp;gt; (binding [*print-level* 1]
         (prn {:a 1 :b 2}))
{:a 1, :b 2}
nil

user=&amp;gt; (binding [*print-level* 1]
         (prn {:a {:b 2}}))
{:a #}
nil

user=&amp;gt; (binding [*print-level* 1]
         (prn [(clojure.lang.MapEntry. :a 1)
               (clojure.lang.MapEntry. :b 2)]))
[# #]
nil

;; clojure.pprint

user=&amp;gt; (binding [*print-level* 1]
         (clojure.pprint/pprint {:a 1 :b 2}))
{#, #}
nil

user=&amp;gt; (binding [*print-level* 1]
         (clojure.pprint/pprint {:a {:b 2}}))
{#}
nil

user=&amp;gt; (binding [*print-level* 1]
         (clojure.pprint/pprint
           [(clojure.lang.MapEntry. :a 1)
            (clojure.lang.MapEntry. :b 1)]))
[# #]
nil
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The docstring for &lt;code&gt;*print-level*&lt;/code&gt; says:&lt;/p&gt;
&lt;blockquote&gt;&lt;p&gt;If an object is a collection and is at a level greater than or equal to the value bound to &lt;em&gt;print-level&lt;/em&gt;, the printer prints '#' to represent it.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;A map entry is a collection and in &lt;code&gt;{:a 1, :b 2}&lt;/code&gt; is at a level equal to &lt;code&gt;*print-level*&lt;/code&gt;, so I would expect &lt;code&gt;prn&lt;/code&gt; to have the same output as &lt;code&gt;clojure.pprint&lt;/code&gt;.&lt;/p&gt;
</description>
<category>Printing</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13337/print-method-inconsistent-regard-clojure-pprint-print-level</guid>
<pubDate>Thu, 28 Sep 2023 07:02:04 +0000</pubDate>
</item>
<item>
<title>problem with Null exception in cl-format</title>
<link>https://ask.clojure.org/index.php/13162/problem-with-null-exception-in-cl-format</link>
<description>&lt;p&gt;If the format string ends in ~@, there is code in cl-format to detect this&lt;br&gt;
and issue a format-error.  However, that error detection is wrong, and a null exception&lt;br&gt;
happens rather than the format-error being reported.&lt;/p&gt;
&lt;p&gt;To reproduce this, simply evaluate the following.&lt;/p&gt;
&lt;p&gt;clojure.pprint&amp;gt; (cl-format false &quot;~@&quot;)&lt;br&gt;
Execution error (NullPointerException) at nrepl.middleware.interruptible-eval/evaluate$fn$fn (interruptible_eval.clj:87).&lt;/p&gt;
&lt;p&gt;I have a fix for this which I'd like to contribute.&lt;/p&gt;
&lt;p&gt;Here is the new version of the code &lt;code&gt;compile-directive&lt;/code&gt;&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn- compile-directive [s offset]
  (let [[raw-params [rest offset]] (extract-params s offset)
        [_ [rest offset flags]] (extract-flags rest offset)
        directive (first rest)
        ;; previous version was missing (if directive ...) here thus (format-error ...) never reached
        def (if directive (get directive-table (Character/toUpperCase ^Character directive)))
        params (if def (map-params def (map translate-param raw-params) flags offset))]
    (if (not directive)
      (format-error &quot;Format string ended in the middle of a directive&quot; offset))
    (if (not def)
      (format-error (str &quot;Directive \&quot;&quot; directive &quot;\&quot; is undefined&quot;) offset))
    [(struct compiled-directive ((:generator-fn def) params offset) def params offset)
     (let [remainder (subs rest 1) 
           offset (inc offset)
           trim? (and (= \newline (:directive def))
                      (not (:colon params)))
           trim-count (if trim? (prefix-count remainder [\space \tab]) 0)
           remainder (subs remainder trim-count)
           offset (+ offset trim-count)]
       [remainder offset])]))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The old version&lt;/p&gt;
</description>
<category>Printing</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13162/problem-with-null-exception-in-cl-format</guid>
<pubDate>Wed, 16 Aug 2023 14:36:19 +0000</pubDate>
</item>
</channel>
</rss>