To get a handle on a Java instance method which we can invoke later on, we can call the memfn function:
user=> (def g (memfn Integer/toString))
#'user/g
user=> (g 789)
"789"
This doesn't 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 can we get a handle to a Java static method, so we can invoke it later on?
(defn g [x] (Integer/toHexString x))
...? If you want, you can wrap that up in a macro, but there's not much left to do:
(defmacro static-fn [f] `(fn [x#] (~f x#)))
(def g (static-fn Integer/toHexString))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.