我可以在Clojure中部分调用Java方法吗?
我有一个对象的方法我可以在Clojure中部分调用Java方法吗?,clojure,clojure-java-interop,Clojure,Clojure Java Interop,我有一个对象的方法 myObject.myMethod(1) 我可以在Clojure中调用它 (.myMethod myObject 1) 我还可以使用词汇环境中的信息调用它 (let [x 1] (.myMethod myObject x)) 我可以用一个局部的吗?例如 (let [myPartial (partial .myMethod myObject)] (myPartial 1)) 这给了我一个机会 java.lang.RuntimeException:无法解析此上
myObject.myMethod(1)
我可以在Clojure中调用它
(.myMethod myObject 1)
我还可以使用词汇环境中的信息调用它
(let [x 1] (.myMethod myObject x))
我可以用一个局部的吗?例如
(let [myPartial (partial .myMethod myObject)]
(myPartial 1))
这给了我一个机会
java.lang.RuntimeException:无法解析此上下文中的符号:.myMethod
我目前正在使用一个匿名函数来完成这项工作
(let [myThing #(.myMethod myObject %)]
(myThing 1))
但是如果在这种情况下使用partial会更好。可能吗
我确信答案将与绑定和分派有关,但我还不知道在编译和执行过程中分派发生在哪里。在您的情况下,可以使用partial,使用() 在答复中:
user=> (doc memfn)
-------------------------
clojure.core/memfn
([name & args])
Macro
Expands into code that creates a fn that expects to be passed an
object and any args and calls the named instance method on the
object passing the args. Use when you want to treat a Java method as
a first-class fn. name may be type-hinted with the method receiver's
type in order to avoid reflective calls.
user=> (doc memfn)
-------------------------
clojure.core/memfn
([name & args])
Macro
Expands into code that creates a fn that expects to be passed an
object and any args and calls the named instance method on the
object passing the args. Use when you want to treat a Java method as
a first-class fn. name may be type-hinted with the method receiver's
type in order to avoid reflective calls.