brandonbloom / fipp

Fast Idiomatic Pretty Printer for Clojure
526 stars 44 forks source link

Strategy to print `goog.math/Long` #91

Open prabhjots opened 1 week ago

prabhjots commented 1 week ago

goog.math/Long is not showing up very well

#?(:cljs
   (let [x (goog.math/Long.fromString "123")]
     (with-out-str (fipp/pprint {:test x} {:width 120}))))

It evaluates to the following

"{:test #object[#object[Long]]}\n"

Problem gets fixed by using the IEdn protocol

#?(:cljs
   (extend-protocol IEdn
     goog.math/Long
     (-edn [x]
       (js/Number (.toString ^goog.math/Long x)))))

Is using the IEdn protocol is the right approach for this problem. If yes then please consider documenting the IEdn protocol in the readme.

brandonbloom commented 6 days ago

Yes, IEdn is the right way to do this. I should probably document it.

However, I think you may want to avoid losing precision by doing something like this:

#?(:cljs
   (extend-protocol IEdn
     goog.math/Long
     (-edn [x]
       (tagged-literal 'goog/long (.toString ^goog.math/Long x)))))