为什么通用方法调用的错误报告中有一个受限制的组件?

huangapple go评论77阅读模式
英文:

why is a component in the error report for generic method call bounded?

问题

在《Java核心技术卷1》中,Horstmann推荐了Peter von der Ahé的这个技巧,即“故意引入一个错误...以查看编译器为泛型方法调用推断出哪种类型”。

例如,对于以下代码:

//class ArrayAlg
public static <T> T getMiddle(T...a) {
return a[a.length/2];
}

将结果分配如下:

JButton aButton = ArrayAlg.getMiddle("Hello", 0, null);

将会产生以下错误报告:

found:
java.lang.Object&java.io.Serializable&java.lang.Comparable<?
       extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>

Horstmann解释说:“用简单的英语来说,你可以将结果分配给ObjectSerializableComparable。”

问题:

我不理解错误报告的最后一部分...&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>

为什么有Comparable组件的边界?

英文:

In 'Core Java Volume 1', Horstmann recommends this trick from Peter von der Ahé to "purposely introducing an error ... to see which type the compiler infers for a generic method call".

For example, for:

//class ArrayAlg
public static &lt;T&gt; T getMiddle(T...a) {
return a[a.length/2];
}

Assigning the result as such:

JButton aButton = ArrayAlg.getMiddle(&quot;Hello&quot;, 0, null);

will give an error report of:

found:
java.lang.Object&amp;java.io.Serializable&amp;java.lang.Comparable&lt;? extends 
   java.lang.Object&amp;java.io.Serializable&amp;java.lang.Comparable&lt;?&gt;&gt;

which Horstmann explains: "In plain English, you can assign the result to Object, Serializable, or Comparable.

Question:

I don't understand the last part of the error report ...&amp;java.lang.Comparable&lt;? extends java.lang.Object&amp;java.io.Serializable&amp;java.lang.Comparable&lt;?&gt;&gt;.

Why is there a bound for Comparable component?

答案1

得分: 0

"为什么Comparable组件有界限?"

TL;DR — 这很复杂。但我很简单地想:所有三者都被报告,因为String(也叫做,T,又叫做“Hello”) 实现了所有三个接口


详细版本

您看到的是编译器三个类型推断过程之一失败的结果

基本上,类型推断试图弄清楚:“嗯,T可能是什么类型?”。它确定_T_是某种数组

然后它必须弄清楚,该_Array_中元素具体类型是什么。为了回答这个问题,它检查了泛型方法的参数。它对于兼容性提出了断言:“由于第一个参数的类型是String,而且我已经知道我正在处理一个Array —— 而且Array总是保存相同的类型 —— 那么其他两个参数在逻辑上也必须是String类型”。

然后,它进行了编译器等价于查看Javadoc的操作,以确定_StringObject&Serializable&Comparable_是兼容的

这是为什么您在错误消息中看到了_Comparable_和其他两个组件的高级用通俗的英语来说”概述。如果需要详细信息,请阅读我上面链接的JLS章节。

英文:

> „&hellip;Why is there a bound for Comparable component?&hellip;

TL;DR — It's complicated. But I think of it very simply: All three are reported because String (aka, T, aka "Hello") implements all three.


The long version

You're seeing the result of a failure of one of the compiler's three type inference processes.

Essentially, type inference tries to figure out: „Hmm, now what type could T be?“. It determines that T is an Array of something.

Then it has to figure out, what the specific type of the elements of that Array is. To answer that, it examines the generic method's argument(s). It makes an assertion about compatibilty: „Since the first argument is of type String, and since I already know I'm dealing with an Array — and Arrays always hold the same type — then the other two arguments, logically, must also be of type String“.

Then it does the compiler equivalent of looking at the Javadoc to figure out that String is compatible with Object &amp; Serializable &amp; Comparable.

That's the high-levelIn plain English“ overview of why you're seeing Comparable and the other two in your error message. Read the chapter of the JLS I linked to above if you need the gory details.

huangapple
  • 本文由 发表于 2020年8月21日 10:57:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63515881.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定