Java’s tostring() is equal to the clojure function

Some of the Java code I use calls toString () on my clojure function object, which returns something like # < ns $something something$ something@7ce1eae7 \u0026gt;> – I want to return something else... There may be a way to include some metadata in the function so that their object toString () returns?

Solution

If you just want to make the repl printing of objects more meaningful, you can implement defmethod print method for the problematic class

This is an abbreviated version of some code I recently wrote, which makes the repl printing of selenium webdriver webdriver object more meaningful:

(defmethod print-method WebDriver
[o w]
(print-simple
 (str "#<" "Title: "    (.getTitle o) ","
           "URL: "      (.getCurrentUrl o) " >")
  w))

Print out like # < Title: a title, URL: http://example.com >

Here, webdriver represents a class; You can easily implement this function for the built-in clojure data structure by implementing the print method for the appropriate class (clojure joy provides the function for the print method of clojure.lang.persistentqueue, which does not have a good representation by default) O above is the actual object you are working on, and W is a writer (these printing functions are required)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>