简体   繁体   English

实现接口的解决方法包括<TIn extends T> , 带 X[] 类型参数

[英]Workarounds to implement interface including <TIn extends T>, with X[] type parameter

Let's say we have some interface like this:假设我们有一些这样的界面:

public interface Foo<T> {

   <TIn extends T> void encode(TIn value)

   T decode()

}

I'm using Foo a lot in my codebase, but I wish to add the TIn extends T to make it more flexible, to eg have a Foo<Map<X>> able to encode a HashMap<X> or a TreeMap<X> .我在我的代码库中经常使用 Foo,但我希望添加TIn extends T以使其更灵活,例如让Foo<Map<X>>能够encode HashMap<X>TreeMap<X>

This works really well - until I tried to implement Foo<T[]> , where it seems to be impossible to implement, with public <TIn extends TItem[]> void encode(TIn array) giving a parse error " > expected " when it hits the brackets [] .这非常有效 - 直到我尝试实现Foo<T[]> ,它似乎无法实现,当public <TIn extends TItem[]> void encode(TIn array)给出解析错误“ > expected ”时它击中括号[] Even IntelliJ does nothing when it volunteers to implement the interface.即使是 IntelliJ 在自愿实现接口时也什么都不做。

For what it's worth, if T is some other concrete final type (eg byte[], Boolean, etc), it seems that I can satisfy the interface by just returning T, so it seems to do some hidden intelligent fixing here.对于它的价值,如果 T 是其他一些具体的最终类型(例如 byte[]、Boolean 等),我似乎可以通过返回 T 来满足接口,所以它似乎在这里做了一些隐藏的智能修复。 So it seems that it's just a problem with T[] where it can't detect T[] is final.所以看起来这只是T[]的问题,它无法检测到T[]是最终的。

Does anyone have any ideas about how I can workaround this?有人对我如何解决这个问题有任何想法吗? I don't really care that TIn extends byte[] can only be met with TIn = byte[] , I just want to implement the interface for the compiler to be happy;我真的不在乎TIn extends byte[]只能满足TIn = byte[] ,我只想实现接口让编译器开心; and so that this interface can be used elsewhere.这样这个接口就可以在其他地方使用。

None of this makes sense.这些都没有意义。 You don't need that TIn in the first place:你首先不需要那个TIn

public interface Foo<T> {
  void encode(T value);
  T decode;
}

class Example {
  void test() {
    Foo<HashMap<String, Integer>> foo = null;
    foo.encode(new HashMap<String, Integer>());
  }
}

This compiles just fine.这编译得很好。 In general if you declare a new typevar that is used in only 1 place, it's pointless - typevars are solely a thing javac worries about, the runtime doesn't know what generics (typevars) are.一般来说,如果您声明一个仅在一个地方使用的新类型变量,那是没有意义的——类型变量只是javac担心的事情,运行时不知道泛型(类型变量)是什么。 Hence, it doesn't make much sense to use them unless they serve to link 2 different places where a type is mentioned, eg 'the type of parameter to the encode method, the return type of the decode() method?因此,除非它们用于链接提到类型的 2 个不同位置,否则使用它们没有多大意义,例如“编码方法的参数类型,解码()方法的返回类型? I don't care what it is, but, for any given usage of the Foo type, it's the same - that kind of 'linking').我不在乎它是什么,但是,对于Foo类型的任何给定用法,它都是一样的——那种“链接”)。

Given that there's no need to introduce an additional type param on the encode method, there's no need to try to declare a new type var there.鉴于无需在encode方法中引入额外的类型参数,因此无需尝试在那里声明新的类型 var。

The usual alternative to a typevar that is used in just a single location is ?仅在单个位置使用的 typevar 的通常替代方法是? . . There is no functional difference between <F> void foo(List<F> in) and void foo(List<?> in) . <F> void foo(List<F> in)void foo(List<?> in)之间没有功能区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM