Clojure使用重载方法对Java接口进行验证 [英] Clojure reify a Java interface with overloaded methods
问题描述
我正在尝试在Clojure中实现以下Java接口:
I'm trying to implement the following Java interface in Clojure:
package quickfix;
public interface MessageFactory {
Message create(String beginString, String msgType);
Group create(String beginString, String msgType, int correspondingFieldID);
}
以下Clojure代码是我尝试执行的操作:
The following Clojure code is my attempt at doing this:
(defn -create-message-factory
[]
(reify quickfix.MessageFactory
(create [beginString msgType]
nil)
(create [beginString msgType correspondingFieldID]
nil)))
此操作无法编译并显示错误:
This fails to compile with the error:
java.lang.IllegalArgumentException:无法在接口中定义方法:创建
java.lang.IllegalArgumentException: Can't define method not in interfaces: create
文档建议重载接口方法是可以的,只要在这种情况下Arity不同即可。
The documentation suggests overloaded interface methods are ok, so long as the arity is different as it is in this case:
如果方法在协议/接口中重载,则必须提供多个
独立的方法定义。如果在接口中由于
相同的重载而过载,则必须指定完整的提示以消除
的歧义-缺少提示暗示对象。
If a method is overloaded in a protocol/interface, multiple independent method definitions must be supplied. If overloaded with same arity in an interface you must specify complete hints to disambiguate - a missing hint implies Object.
如何使它正常工作?
推荐答案
您缺少参数。 reify
实现的每个方法的第一个参数是对象本身(与 defrecord
/ <$ c一样) $ c> deftype )。因此,尝试以下操作:
You're missing a parameter. The first parameter of every method implemented by reify
is the object itself (as is the case with defrecord
/deftype
). So, try this:
(defn -create-message-factory
[]
(reify quickfix.MessageFactory
(create [this beginString msgType]
nil)
(create [this beginString msgType correspondingFieldID]
nil)))
这篇关于Clojure使用重载方法对Java接口进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!