解决界面的解决方法,包括< tin扩展了t>,t>,x []类型参数
假设我们有这样的界面:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这些都没有道理。首先,您不需要
tin
:这可以很好地编译。通常,如果您声明仅在1个位置使用的新typeVar,则它是毫无意义的 - typevars完全是
javac
担心的东西,运行时不知道generics(typevars)是什么。因此,除非它们用来链接提及类型的2个不同位置,例如'expode方法的参数类型,即解码()方法的返回类型,否则使用它们是没有多大意义的吗?我不在乎它是什么,但是,对于任何给定的foo
类型的用法,它是相同的 - 这种“链接”)。鉴于无需在
encode
方法上引入附加类型的参数,因此无需尝试在其中声明新的类型var。仅在单个位置中使用的TypeVar的通常替代方法是
?
。&lt; f&gt; void foo(list&lt; f&gt; in)
andvoid foo(list&lt;?&gt; in)
。None of this makes sense. You don't need that
TIn
in the first place: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 theFoo
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)
andvoid foo(List<?> in)
.