- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
考虑以下代码示例:
@SafeVarargs
public static <U> Object[] sortedCopy(Comparator<? super U> comparator, U... values) {
U[] copy = Arrays.copyOf(values, values.length);
Arrays.sort(copy, comparator);
return copy; //copy is implicitly cast to Object[] -> no heap pollution
}
public static <U> Object[] sortedCopy(U... values) {
return sortedCopy(Comparator.naturalOrder(), values); //why does this compile???
}
我本以为编译器会拒绝 sortedCopy(U...)
中的行, 推理如下:
Comparator.naturalOrder()
的返回类型是Comparator<T>
, 其中T
是此方法的泛型类型参数,必须满足约束 T extends Comparable<? super T>
.由于该方法不接受任何参数,并且其在上述代码中的调用并未明确指定 T
的类型。 (比如 Comparator.<U>naturalOrder()
,因为 U
没有扩展 Comparable
而无法编译), T
必须以另一种方式推断。我能看到推断的唯一方法T
是通过 sortedCopy(Comparator<? super U>, U...)
的方法签名.编译器知道 values
的类型从而可以推断出 U
, 反过来也可以推断出 T
的边界, 即有界通配符 ? super U
.但是,编译器应该意识到任何 ? super U
永远无法满足 T
的要求由 Comparator.naturalOrder()
指定,即 T extends Comparable<? super T>
,因为 U
本身不扩展 Comparable<? super U>
, 所以 U
的任何父类(super class)型也不能。
让我感到困惑的是,当我从 sortedCopy(U...)
更改签名时,编译器确实 生成错误至 sortedCopy(U[])
.我想这与以下事实有关,在第二种情况下,U
在运行时作为数组的类型存在,而在第一种情况下,它不是。但我仍然不明白为什么这会使有问题的方法调用有效,因为:
Object[]
。如果作为 vararg 参数传递给方法的值是通用的,因此是不可具体化的类型,如果我理解正确的话,上面的代码就是这种情况,因为 U
来自 sortedCopy(U...)
是不可具体化的。但即便如此,为什么编译器没有意识到 Object
不扩展 Comparable<? super Object>
?U
可能在运行时不再存在,编译器仍然知道它并且应该能够检查是否满足等式约束,无论方法参数是数组还是可变参数。那么为什么上面代码示例中的问题行仍然可以编译?
除此之外,对于 @SafeVarargs
的反馈,我也将不胜感激。方法注释 sortedCopy(Comparator<? super U>, U...)
是不合适的。我相信是的,但我对此没有信心。
最佳答案
Varargs 可能有点偷偷摸摸。我在我的 IDE 中看到的是关于 sortedCopy(U... values)
作为递归 方法,这意味着它不会选择带有 Comparator
的重载参数作为它的第一个参数。
如果将其从可变参数更改为数组参数,并传入 int[]
,您会遇到预期的编译失败。
Error:(12, 16) no suitable method found for sortedCopy(java.util.Comparator<T>,U[])
method Foo.<U>sortedCopy(java.util.Comparator<? super U>,U[]) is not applicable
(inferred type does not conform to upper bound(s)
inferred: U
upper bound(s): java.lang.Comparable<? super U>,U,java.lang.Object)
method Foo.<U>sortedCopy(U[]) is not applicable
(cannot infer type-variable(s) U
(actual and formal argument lists differ in length))
Error:(18, 47) no suitable method found for sortedCopy(java.util.Comparator<T>,int[])
method Foo.<U>sortedCopy(java.util.Comparator<? super U>,U[]) is not applicable
(inference variable U has incompatible bounds
equality constraints: int
upper bounds: java.lang.Object)
method Foo.<U>sortedCopy(U[]) is not applicable
(cannot infer type-variable(s) U
(actual and formal argument lists differ in length))
如果你传入一个 Integer[]
,或者如果您仅传入可变参数形式,则只会出现第一个错误。
关于picking the most specific method有规则可供词法分析器使用。具体来说,您会被这条规则所困扰:
A type S is more specific than a type T for any expression if S <: T.
更详细:
The subtypes of a type T are all types U such that T is a supertype of U, and the null type.
据此,我们推断,因为泛型不是良好绑定(bind)的,它正在接受 Comparable
作为可变参数的一部分。 这就是为什么我的 IDE 选择它作为递归方法,以及为什么当我运行它时我得到一个 StackOverflowError
.
如果您将泛型正确绑定(bind)到 <U extends Comparable<U>>
,这个问题实际上就会消失以确保它不会拾取任何你实际上无法分类的东西......
public static <U extends Comparable<U>> Object[] sortedCopy(Comparator<? super U> comparator, U... values) {
U[] copy = Arrays.copyOf(values, values.length);
Arrays.sort(copy, comparator);
return copy;
}
public static <U extends Comparable<U>> Object[] sortedCopy(U... values) {
return sortedCopy(Comparator.naturalOrder(), values);
}
...警告您可能现在有堆污染,引入固定数量的方法会更简单、更简洁。
关于java - 在方法参数中强制执行泛型类型相等性约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45535418/
我在 Mac OsX 10.11 上使用 Xcode 7.0.1 (7A1001) 我使用 carthage 0.9.2 通过以下购物车文件下载reactivecocoa github“Reactiv
我正在将一个对象从属性“模型”(我从 Laravel 中的 Blade 属性模型中获得)分配给数据属性模型。后来数据属性模型发生变化,因为它绑定(bind)到表单输入字段。但 Prop “模型”也发生
当我更新数组内对象的属性然后作为组件的 Prop 传递时,在 svelte 中触发 react 性的正确方法是什么? let items = [{ id: 1, name: 'first'
我是 DRY principle 的坚定拥护者: Every piece of knowledge must have a single, unambiguous, authoritative rep
我正在实现一个需要以下功能的线程: 及时响应终止请求 推送消息 在等待消息时保持对 SendMessage 请求的响应 我对消息泵的初始实现使用了 GetMessage,如下所示: while not
在我的应用程序中,用户获得了一份已到达她的文档列表,并且可以对每个文档执行操作。 文件是分批提交的,当这种情况发生时,列表会增加。这一切都很好,这是预期的行为,但最好有一个按钮“暂停实时数据”,它会忽
我有一个属性为 的数据对象 displaySubtotal 我可以通过以下方式更新该属性的值: data.displaySubtotal = numPad.valueAsAString(); 我的方法
我需要一个垂直 slider 输入。由于内置的 sliderInput 函数无法做到这一点,因此我选择自己实现。根据this thread可以 (I) 使用 CSS 旋转 sliderInput
我正在从自定义用户权限管理系统迁移到 Alanning:roles v2.0 .我有一个非常基本的结构: 基本用户 用户组,每个用户组都有特定的设置。我将它们存储在一个“组”集合中。 管理群组的用户的
Shiny 中的响应式(Reactive)表达式将更改传播到需要去的地方。我们可以使用 isolate 来抑制一些这种行为。 ,但是我们可以抑制基于我们自己的逻辑表达式传播的更改吗? 我给出的例子是一
是否有(或可能有) react 性 Parsec (或任何其他纯函数式解析器)在 Haskell 中? 简而言之,我想逐个字符地为解析器提供数据,并获得与我提供的足够多的结果一样多的结果。 或者更简单
HTML(JADE) p#result Lorem ipsum is javascript j s lo 1 2 4 this meteor thismeteor. meteor input.sear
我有一个被导入函数更改的对象。 https://svelte.dev/repl/e934087af1dc4a25a1ee52cf3fd3bbea?version=3.12.1 我想知道如何使我的更改反
我有一个YUV 420半平面格式的图像,其中字节以这种方式存储: [Y1 Y2 ... [U1 V1.... Yk Yk+1...] Uk' Uk'+1] 其中Y平面的大小是UV平面的两倍,并
如何使用 ReactiveCocoa 订阅从 NSMutableDictionary 添加和删除的对象?另外,我想在它发生变化时广播通知。我的猜测是可以使用 RACMulticastConnectio
我正在构建一个带有多个选项卡的应用程序,其中一些选项卡涉及过多的计算,而另一些选项卡的计算速度很快。一个允许用户在 react 性或手动更新之间进行选择的复选框,与“刷新”按钮结合使用,将是理想的选择
我知道您可以在获取集合时使用 reactive: false 关闭 react 性。如何在内容可编辑区域内的集合字段中实现相同的效果?示例: Template.documentPage.events(
我想在 z3 中表示一个哈希函数,比如 SHA(x)。在做了一些研究之后,似乎 z3 不能很好地支持注入(inject)性,所以我不能有像这样的约束(虽然我意识到这并不是严格意义上的碰撞,但作为一种启
我正在解决一个问题,我想在仪表板中将数据显示为图表(通过 perak:c3 )和表格(通过 aslagle:reactive-table )。我的问题是数据是从 MongoDB 中的集合中提取的,它的
我的 ViewModel 中有这个函数,它返回一个信号,但内部 block 不起作用,我尝试添加断点,但它没有中断。这是我的代码。 func executeLoginAPI() -> RACSigna
我是一名优秀的程序员,十分优秀!