Joe*_*Joe 10 clojure clojure-java-interop
我有一个对象的方法.
myObject.myMethod(1)
Run Code Online (Sandbox Code Playgroud)
我可以在Clojure中调用它
(.myMethod myObject 1)
Run Code Online (Sandbox Code Playgroud)
我也可以使用词汇环境中的信息来调用它
(let [x 1] (.myMethod myObject x))
Run Code Online (Sandbox Code Playgroud)
我可以部分地这样做吗?例如
(let [myPartial (partial .myMethod myObject)]
(myPartial 1))
Run Code Online (Sandbox Code Playgroud)
这给了我一个
java.lang.RuntimeException:无法在此上下文中解析符号:.myMethod
我目前正在使用匿名函数进行此工作
(let [myThing #(.myMethod myObject %)]
(myThing 1))
Run Code Online (Sandbox Code Playgroud)
但是如果在这种情况下使用部分会很好.可能吗?
我确定答案将是绑定和发送,但我还没有感觉在编译和执行期间发生的调度.
Chi*_*ron 11
在你的情况下你可以使用(memfn).
(memfn myMethod args)
Run Code Online (Sandbox Code Playgroud)
在REPL中:
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.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
489 次 |
| 最近记录: |