Welcome! Please see the About page for a little more info on how this works.

+1 vote
in Printing by

There seams to be a bug in clojure.pprint/pprint when using the code-dispatch table and printing let. It happens on Clojure and ClojureScript.

Clojure 1.11.1
user=> (require '[clojure.pprint :as pp])

(binding [pp/*print-pprint-dispatch* pp/code-dispatch]

((let)) ;; prints some extra parenthesis

(binding [pp/*print-pprint-dispatch* pp/code-dispatch]

(let*) ;; doesn't happen when pprinting any other form

2 Answers

+1 vote

So the problem is that in clojure.pprint/pprint-let the pprint-logical-block wraps both branches while it shouldn't since pprint-simple-code-list already generate parenthesis.

Adding a patch here :

From 1761194420c8befe62a6e12cabfd4483b645b703 Mon Sep 17 00:00:00 2001                                
From: Juan Monetta <jpmonettas@gmail.com>                                                             
Date: Fri, 10 Nov 2023 11:19:02 -0300                                                                 
Subject: [PATCH] Fix clojure.pprint pprint-let when form doesn't contain                               
 src/clj/clojure/pprint/dispatch.clj | 15 ++++++++-------                                               
 1 file changed, 8 insertions(+), 7 deletions(-)                                                         
diff --git a/src/clj/clojure/pprint/dispatch.clj b/src/clj/clojure/pprint/dispatch.clj                
index 965c0b25..cd1c2b29 100644                                                                        
--- a/src/clj/clojure/pprint/dispatch.clj                                                              
+++ b/src/clj/clojure/pprint/dispatch.clj                                                             
@@ -335,13 +335,14 @@                                                                                 
 (defn- pprint-let [alis]                                                                              
   (let [base-sym (first alis)]                                                                        
-    (pprint-logical-block :prefix "(" :suffix ")"                                                    
-      (if (and (next alis) (vector? (second alis)))                                                    
-        (do                                                                                            
-          ((formatter-out "~w ~1I~@_") base-sym)                                                     
-          (pprint-binding-form (second alis))                                                        
-          ((formatter-out " ~_~{~w~^ ~_~}") (next (rest alis))))                                      
-        (pprint-simple-code-list alis)))))                                                            
+    (if (and (next alis) (vector? (second alis)))                                                    
+      (pprint-logical-block                                                                          
+       :prefix "(" :suffix ")"                                                                        
+       (do                                                                                            
+         ((formatter-out "~w ~1I~@_") base-sym)                                                       
+         (pprint-binding-form (second alis))                                                           
+         ((formatter-out " ~_~{~w~^ ~_~}") (next (rest alis)))))                                       
+      (pprint-simple-code-list alis))))                                                                
Would you like to add an official patch to the Jira ticket?
I'm not sure I can, since I don't have a Jira user
You can follow the contributor instructions [here](https://clojure.org/dev/dev) if you would like. There is no pressure to do so, I just wanted to suggest it since you had already worked out a possible fix.
0 votes

jira ticket https://clojure.atlassian.net/browse/CLJ-2816 was created to track this issue.