简体   繁体   English

如何在另一个有界类型上定义方法有界类型?

[英]How do I defined a method bounded type on another bounded type?

I want to define a method that defines its types like this: 我想定义一个定义其类型的方法,如下所示:

List<R> toList(JsArray<T> array)

such that T is bounded such that it is both: 这样T的界限就是:

  • T extends SomeClass , and T extends SomeClass ,和
  • T extends R

I've tried things like this, to no avail: 我尝试过这样的事情,但无济于事:

<R, T extends R & SomeClass> List<R> toList(JsArray<T> array)

(For the curious, this is to be able to use GWT Overlay Types with Interfaces ) (对于好奇,这是能够使用带接口的GWT覆盖类型

How about this? 这个怎么样?

<T extends SomeClass> List<? super T> toList(JsArray<T> array)

EDIT: 编辑:

I think I see your use case. 我想我看到了你的用例。 From comments, you want to specify the return type so you don't have an unchecked cast. 从注释中,您需要指定返回类型,以便不进行未选中的强制转换。

But you don't actually pass anything in to tell the method what to create. 但是你实际上没有传递任何东西来告诉方法要创建什么。 Since I could always call toList() with R = T , the only thing you could do that always works (for all R super T ) is to return a List<T> , which makes the R parameter unnecessary... but that's not what you want. 因为我总是可以用R = T来调用toList(),所以你唯一可以做的就是(对于所有R super T )是返回List<T> ,这使R参数变得不必要......但那不是你想要什么。

You have to pass something in to tell the method what kind of object to instantiate. 你必须传递一些东西告诉方法要实例化什么样的对象。 Usually we could to that like: 通常我们可以这样:

<R, T extends SomeClass> List<R> toList(JsArray<T> array, Class<R> cls)

and call it like 并称之为

toList(arrayIHave, WhatIWant.class)

Now this will work, but you will complain that it has error conditions, because the " T extends R " constraint isn't captured. 现在这将工作,但你会抱怨它有错误条件,因为没有捕获“ T extends R ”约束。 But really, even if you add the constraint in, you'll still have error conditions. 但实际上,即使你添加约束,你仍然会有错误条件。

I can use an arbitrary interface for R by making a T that extends SomeClass and implements R . 我可以通过创建一个扩展SomeClass并实现RT来为R使用任意接口。 There is no way you can meaningfully instantiate an arbitrary interface, so for many R,T pairs that satisfy your constraints, you will have to return null or throw an exception or something. 您无法有意义地实例化任意接口,因此对于满足约束的许多R,T对,您将不得不返回null或抛出异常或其他内容。

No doubt there is some rule that dictates which R you will return, but there is no way to even think about specifying generic constraints that capture that rule, so there will be an unchecked conversion somewhere. 毫无疑问,有一些规则,规定了哪些R您将返回,但没有办法,甚至考虑指定捕获即治泛型约束,这样就会有一个未经检查的转换地方。 If you use a signature like this, at least you can put it inside the method instead of having warnings or annotations everywhere you use it. 如果您使用这样的签名,至少可以将其放在方法中,而不是在您使用它的任何地方都有警告或注释。

I fear this does not work. 我担心这不起作用。 The correct syntax would be 正确的语法是

<R, T extends R & SomeClass> List<R> toList(JsArray<T> array)

But this will give you the following error message 但是这会给你以下错误信息

Cannot specify any additional bound SomeClass when first bound is a type parameter 第一次绑定时,无法指定任何其他绑定SomeClass是类型参数

I do not see how you could make it work to enforce both constraints. 我看不出你如何能够强制执行这两个约束。

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

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