0

我正在尝试实现org.joda.time.ReadableInstant。它继承自通用接口,但显然这无关紧要。界面是:

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 邮件列表上的人,我已经问过这个问题的较长版本,我认为这里的较短问题可能更容易回答)

4

1 回答 1

2

您不能使用 defrecord 来实现带有get方法的类型,因为get已经在 java.util.Map 上定义了,defrecord 会自动为您实现。如果你想实现这个接口,你将不得不放弃 mappiness 的细节,而只使用一个普通的 deftype。此外,您的代码中的每个类型提示都是完全不必要的:编译器知道您正在实现的接口的类型,并且不需要您的帮助来弄清楚它们。

于 2014-09-11T10:24:57.467 回答