Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm also not sure why the restriction is there. You could try sending a friendly e-mail to the designers of Java 5 Generics (chiefly Gilad Bracha and Neal Gafter).</p> <p>My guess is that they wanted to support only an absolute minimum of <a href="https://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.9" rel="noreferrer">intersection types</a> (which is what multiple bounds essentially are), to make the language no more complex than needed. An intersection cannot be used as a type annotation; a programmer can only express an intersection when it appears as the upper bound of a type variable.</p> <p>And why was this case even supported? The answer is that multiple bounds allow you to control the erasure, which allows to maintain binary compatibility when generifying existing classes. As explained in section 17.4 of the <a href="http://java-generics-book.dev.java.net/" rel="noreferrer">book</a> by Naftalin and Wadler, a <code>max</code> method would logically have the following signature:</p> <pre><code>public static &lt;T extends Comparable&lt;? super T&gt;&gt; T max(Collection&lt;? extends T&gt; coll) </code></pre> <p>However, this erases to:</p> <pre><code>public static Comparable max(Collection coll) </code></pre> <p>Which does not match the historical signature of <code>max</code>, and causes old clients to break. With multiple bounds, only the left-most bound is considered for the erasure, so if <code>max</code> is given the following signature:</p> <pre><code>public static &lt;T extends Object &amp; Comparable&lt;? super T&gt;&gt; T max(Collection&lt;? extends T&gt; coll) </code></pre> <p>Then the erasure of its signature becomes:</p> <pre><code>public static Object max(Collection coll) </code></pre> <p>Which is equal to the signature of <code>max</code> before Generics.</p> <p>It seems plausible that the Java designers only cared about this simple case and restricted other (more advanced) uses of intersection types because they were just unsure of the complexity that it might bring. So the reason for this design decision does not need to be a possible safety problem (as the question suggests).</p> <p>More discussion on intersection types and restrictions of generics in an <a href="http://www.cs.rice.edu/~javaplt/papers/oopsla2008.pdf" rel="noreferrer">upcoming OOPSLA paper</a>.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload