2014-09-11 100 views
0

我正試圖實施org.joda.time.ReadableInstant。它從一個通用接口繼承,但顯然這應該不重要。 The interface是:如何用Clojure記錄實現這個通用Java接口?

public interface ReadableInstant extends Comparable<ReadableInstant> { 
    long getMillis(); 
    Chronology getChronology(); 
    DateTimeZone getZone(); 
    int get(DateTimeFieldType type); 
    boolean isSupported(DateTimeFieldType field); 
    Instant toInstant(); 
    boolean isEqual(ReadableInstant instant); 
    boolean isAfter(ReadableInstant instant); 
    boolean isBefore(ReadableInstant instant); 
    boolean equals(Object readableInstant); 
    int hashCode(); 
    String toString(); 
} 

我的記錄:

(defrecord WeirdDate [year month day] 
    ReadableInstant 
    (^boolean equals [this ^Object readableInstant] (.equals (as-date this) readableInstant)) 
    (^int get [this ^DateTimeFieldType type] (get (as-date this) type)) 
    (^Chronology getChronology [this] (.getChronology (as-date this))) 
    (^long getMillis [this] (.getMillis (as-date this))) 
    (^DateTimeZone getZone [this] (.getZone (as-date this))) 
    (^int hashCode [this] (.hashCode (as-date this))) 
    (^boolean isAfter [this ^ReadableInstant instant] (.isAfter (as-date this) instant)) 
    (^boolean isBefore [this ^ReadableInstant instant] (.isBefore (as-date this) instant)) 
    (^boolean isEqual [this ^ReadableInstant instant] (.isEqual (as-date this) instant)) 
    (^boolean isSupported [this ^DateTimeFieldType field] (.isSupported (as-date this) field)) 
    (^Instant.toInstant [this] (.toInstant (as-date this))) 
    (^String toString [this] (.toString (as-date this)))) 

但我得到的錯誤:

java.lang.IllegalArgumentException: Must hint overloaded method: get 

是我喜歡的類型提示錯誤?還有什麼不對嗎?

(道歉對於那些你在Clojure mailing list where I've already asked a longer version of this question,我認爲這裏更短的問題可能是比較容易回答)

回答

2

您不能使用defrecord來實現與get方法的類型,因爲get是已經在java.util.Map上定義,defrecord會自動爲您執行。如果你想實現這個接口,你將不得不放棄mappiness的精妙,而只是使用普通的deftype。而且,代碼中的每個類型提示都是不必要的:編譯器知道你正在實現的接口的類型,而不需要你的幫助來解決它。

+0

重新鍵入註釋,我很懷疑,只是這條消息似乎在說'添加更多註釋!'。 – Joe 2014-09-11 10:27:13

+0

非常感謝您的回答,就是這樣。我遇到了另一個問題,也許你可以花一分鐘時間? http://stackoverflow.com/questions/25786493/classnotfound-error-trying-to-implement-clojure-protocol-with-a-deftype – Joe 2014-09-11 11:40:57