如何使用Clojure记录实现此通用Java接口? [英] How do I implement this generic Java interface with a Clojure record?

查看:297
本文介绍了如何使用Clojure记录实现此通用Java接口?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实施 org.joda.time。 ReadableInstant 。它继承自一个通用的接口,但显然不应该。 界面 is:

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

我的记录:

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

但我得到错误: p>

But I get the error:

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

我的类型提示是否有错误?是否有其他错误?

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

(对于您在 Clojure邮件列表,我已经问过这个问题的更长版本,我想这里一个更简单的问题可能更容易回答)

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

推荐答案

您不能使用defrecord来实现 get 方法的类型,因为 get 已在java.util.Map中定义,defrecord会自动为您实现。如果你想实现这个接口,你将不得不放弃mappiness的奇怪,只是使用一个简单的deftype。此外,你的代码中的每个类型提示是完全不必要的:编译器知道你正在实现的接口的类型,并且不需要你的帮助来计算出来。

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.

这篇关于如何使用Clojure记录实现此通用Java接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆