9

There used to be this useful utility called show in clojure.contrib. Now, that it's deprecated, is there an equivalent to it?

Thanks!

konr
  • 2,545
  • 2
  • 20
  • 38

2 Answers2

11

De-constructing show to be more "simple", making available distinct pieces of re-usable functionality, was discussed by Stuart Halloway in a talk he give on clojure simplicity.

The resulting code makes use of clojure.reflect/reflect and clojure.pprint/print-table and standard clojure filter:

(require 'clojure.reflect)
(require 'clojure.pprint)

(->> (clojure.reflect/reflect java.lang.String)
     :members
     (filter #(.startsWith (str (:name %)) "last"))
     (clojure.pprint/print-table))
Alex Stoddard
  • 8,244
  • 4
  • 41
  • 61
  • One of the cool things about `show` is that it worked on instances as well as on types (see https://stackoverflow.com/questions/5821286). Are you aware of any examples (e.g. in Clojuredocs or blogs) that show how to pprint the values of instance variables of an instance of a Java class? The clojure.repl namespace doesn't seem to have enough (https://clojure.github.io/clojure/clojure.repl-api.html) – Reb.Cabin Jun 17 '17 at 14:43
  • clojure.reflect/reflect above is built on top of the Java reflection API so it should do what you need. Part of the point of breaking apart "show" was it was doing too much. The reflect functionality was made available in the clojure.reflect namespace (rather than the repl-api). Try the above example with a string instance instead of the type - (clojure.reflect/reflect "instance of a string") – Alex Stoddard Jun 19 '17 at 14:26
6

I refer you to the Where Did Clojure.Contrib Go document, which says about clojure.contrib.repl-utils:

Migrated to clojure.repl and clojure.java.javadoc. show functionality similar to clojure.reflect/reflect.

The clojure.reflect API documentation is here, and the clojuredocs.org examples are here.

Jeremy
  • 22,188
  • 4
  • 68
  • 81