some Java code I'm using invokes toString()
on my Clojure function objects, which return something like #<ns$something something.something$something@7ce1eae7>>
- I want to return something else...presumably there's a way to include some metadata in the functions so their objects' toString()
returns that instead ?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
If you just want to make the REPL print-out of your objects more meaningful, you can implement a
defmethod print-method
for the class in question.Here's a shortened version of some code I've written recently; this makes the REPL print-out of a Selenium-WebDriver WebDriver object more meaningful:
This prints out like
#<Title: A Title, URL: http://example.com >
Here,
WebDriver
represents a class; you can just as easily do this for built-in Clojure data structures by implementingprint-method
for the appropriate class (The Joy of Clojure features aprint-method
forclojure.lang.PersistentQueue
which has no nice representation by default). Theo
above is the actual object you're dealing with andw
is a writer (required by these kinds of print functions).AFAIK, there's no simple way of doing this, though you can use reify or proxy to implement clojure.lang.IFn - which really just amounts to wrapping your function object in another object.
I don't think there's anything you can do about this: there's no way to ensure that your functions implement toString() differently without diving in and re-engineering the internals of Clojure.
However I think you can work around this problem quite easily - I'd suggest wrapping your functions within another class that you control (e.g. as defined through defrecord) before passing to Java code - then you will be able to provide your own custom toString() method, as well as get all the other advantages of using a record.