How to get a clojure handle on a Java static method, similar to_ memfn_ Methods for Java instances?
•
Java
To handle the Java instance methods that we can call later, we can call the memfn function:
user=> (def g (memfn Integer/toString)) #'user/g user=> (g 789) "789"
This does not work for Java static methods:
user=> (def g (memfn Integer/toHexString)) #'user/g user=> (g 789) IllegalArgumentException No matching method found: toHexString for class java.lang.Long clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:52) user=> (g) ArityException Wrong number of args (0) passed to: user$g clojure.lang.AFn.throwArity (AFn.java:437)
How do we get the handle to the Java static method so that it can be called later?
Solution
(defn g [x](Integer / toHexString x))……? You can wrap it in a macro if you like, but there's nothing to do:
(defmacro static-fn [f] `(fn [x#] (~f x#))) (def g (static-fn Integer/toHexString))
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
二维码