在clojure中取消引用java方法
如何在Clojure中参数化调用方法
例如:
(def url (java.net.URL. "http://www.google.com"))
(.getHost url) ;; works!
(def host '.getHost)
(host url) ;; Nope :(
(~host url) ;; Nope :(
(eval `(~host url)) ;; Works :s
你可以在下面搜索框中键入要查询的问题!
如何在Clojure中参数化调用方法
例如:
(def url (java.net.URL. "http://www.google.com"))
(.getHost url) ;; works!
(def host '.getHost)
(host url) ;; Nope :(
(~host url) ;; Nope :(
(eval `(~host url)) ;; Works :s
# 1 楼答案
正确的解决方案:
# 2 楼答案
如果只想为现有函数创建别名,只需要一个包装函数:
虽然你可以像另一个用户所指出的那样使用
memfn
,但发生的事情似乎不那么明显。事实上,即使是克罗朱尔。org建议现在不要这样做:https://clojure.org/reference/java_interop
# 3 楼答案
在Java类上参数化方法的常规方法是:
或者
或者如果对象或参数都不固定
常用于高阶函数的线映射、滤波和归约
# 4 楼答案
使用
memfn
: