解决界面的解决方法,包括< tin扩展了t>,t>,x []类型参数

发布于 2025-02-07 12:59:04 字数 805 浏览 2 评论 0原文

假设我们有这样的界面:

public interface Foo<T> {

   <TIn extends T> void encode(TIn value)

   T decode()

}

我在代码库中使用了很多foo,但是我想添加tin扩展t以使其更灵活,例如具有foo&lt ; map&lt; x&gt;&gt;能够编码 a hashmap&lt; x&gt;treemap&lt; x&gt;

这真的很好 - 直到我尝试实现foo&lt; t []&gt;,似乎无法使用public&lt; tin扩展了titem []&gt; void Encode(Tin Array)给出解析错误“ &gt; enduce”击中括号[]时。即使Intellij自愿实现接口,甚至Intellij也无能为力。

对于它的价值,如果T是其他一些具体的最终类型(例如字节[],布尔值等),则似乎我只能通过返回t来满足界面,因此似乎在这里可以做一些隐藏的智能修复。因此,看来这只是t []无法检测到t []的问题。

有人对我如何解决这个问题有任何想法吗?我真的不在乎tin扩展字节[]只能使用tin = byte [],我只想实现界面,以使编译器快乐;因此,该接口可以在其他地方使用。

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>.

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 []. Even IntelliJ does nothing when it volunteers to implement the interface.

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. So it seems that it's just a problem with T[] where it can't detect T[] is final.

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; and so that this interface can be used elsewhere.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我纯我任性 2025-02-14 12:59:04

这些都没有道理。首先,您不需要 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>());
  }
}

这可以很好地编译。通常,如果您声明仅在1个位置使用的新typeVar,则它是毫无意义的 - typevars完全是javac担心的东西,运行时不知道generics(typevars)是什么。因此,除非它们用来链接提及类型的2个不同位置,例如'expode方法的参数类型,即解码()方法的返回类型,否则使用它们是没有多大意义的吗?我不在乎它是什么,但是,对于任何给定的foo类型的用法,它是相同的 - 这种“链接”)。

鉴于无需在encode方法上引入附加类型的参数,因此无需尝试在其中声明新的类型var。

仅在单个位置中使用的TypeVar的通常替代方法是&lt; f&gt; void foo(list&lt; f&gt; in) and void foo(list&lt;?&gt; in)

None of this makes sense. You don't need that TIn in the first place:

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. Hence, it doesn't make much sense to use them unless they serve to link 2 different places where a type is mentioned, e.g. 'the type of parameter to the encode method, the return type of the decode() method? I don't care what it is, but, for any given usage of the Foo type, it's the same - that kind of 'linking').

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.

The usual alternative to a typevar that is used in just a single location is ?. There is no functional difference between <F> void foo(List<F> in) and void foo(List<?> in).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文