2014-01-08 39 views
3

我試圖實現Clojure中的下列Java接口:Clojure的具體化的Java接口與重載方法

package quickfix; 

public interface MessageFactory { 
    Message create(String beginString, String msgType); 
    Group create(String beginString, String msgType, int correspondingFieldID); 
} 

以下的Clojure代碼是我在做這樣的嘗試:

(defn -create-message-factory 
    [] 
    (reify quickfix.MessageFactory 
    (create [beginString msgType] 
     nil) 
    (create [beginString msgType correspondingFieldID] 
     nil))) 

這失敗,出現錯誤編譯:

java.lang.IllegalArgumentException: Can't define method not in interfaces: create

documentation表明OV erloaded接口方法都行,只要是元數不同,因爲它是在這種情況下:

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.

我怎樣才能得到這個工作?

回答

5

你錯過了一個參數。由reify實施的每種方法的第一個參數都是對象本身(與defrecord/deftype一樣)。所以,試試這個:

(defn -create-message-factory 
    [] 
    (reify quickfix.MessageFactory 
    (create [this beginString msgType] 
     nil) 
    (create [this beginString msgType correspondingFieldID] 
     nil))) 
+0

太棒了。非常感謝 :) –