How do I implement this generic Java interface with a Clojure record?

白昼怎懂夜的黑 提交于 2020-01-25 16:40:06

问题


I'm trying to implement org.joda.time.ReadableInstant. It inherits from a generic interface, but apparently that shouldn't matter. The interface is:

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();
}

My record:

(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))))

But I get the error:

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

Are my type hints wrong? Is there something else wrong?

(Apologies for those of you on the Clojure mailing list where I've already asked a longer version of this question, I thought that a shorter question here might be easier to answer)


回答1:


You can't use a defrecord to implement a type with a get method, because get is already defined on java.util.Map, which defrecord automatically implements for you. If you want to implement this interface, you will have to forego the niceties of mappiness and just use a plain deftype. Also, every type hint in your code is completely unnecessary: the compiler knows the types of the interface you're implementing, and doesn't need your help to figure them out.



来源:https://stackoverflow.com/questions/25783142/how-do-i-implement-this-generic-java-interface-with-a-clojure-record

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!