<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#FFFFFF">
Hi Sam,<br>
This works in JDK 8 (I think there's even a comment in SO saying
that). Long story short, Java 5/6/7 inference typically ignored
inference constraints coming from lower bounds; the treatment in
Java 8 is much more consistent - as a result this program compiles
as you'd expect (assuming you don't use -source 7).<br>
<br>
Maurizio<br>
<br>
<div class="moz-cite-prefix">On 03/01/15 00:07, Sam Munkes wrote:<br>
</div>
<blockquote
cite="mid:CABjcb6VRM8xoyUeS_TeBQX-V+_Hqws_=7ia6QtyGrBWtMw0j+w@mail.gmail.com"
type="cite">
<div dir="ltr">
<div>
<div>
<div>Hi Guys,<br>
<br>
</div>
A colleague of mine recently posted the following question
to StackOverflow: <a moz-do-not-send="true"
href="http://stackoverflow.com/questions/27652867/java-type-inference-with-lower-bounded-types">http://stackoverflow.com/questions/27652867/java-type-inference-with-lower-bounded-types</a><br>
<br>
</div>
Does anyone on the list have insight into why the compiler
does not infer lower bounded types?<br>
<br>
<span style="font-family:monospace,monospace">static class
Test {<br>
   static <T> T pick(T one, T two) {<br>
       return two;<br>
   }<br>
<br>
   static void testUpperBound() {<br>
       List<? extends Integer> extendsInteger = new
ArrayList<>();<br>
<br>
       // List<? extends Integer> is treated as a
subclass of List<? extends Number><br>
       List<? extends Number> extendsNumber =
extendsInteger;<br>
<br>
       // List<? extends Number> is inferred as the
common superclass<br>
       extendsNumber = pick(extendsInteger, extendsNumber);<br>
   }<br>
<br>
   static void testLowerBound() {<br>
       List<? super Number> superNumber = new
ArrayList<>();<br>
<br>
       // List<? super Number> is treated as a
subclass of List<? super Integer><br>
       List<? super Integer> superInteger =
superNumber;<br>
<br>
       // The inferred common type should be List<?
super Integer>,<br>
       // but instead we get a compile error:<br>
       <b>superInteger = pick(superNumber, superInteger);</b><br>
<br>
       // It only compiles with an explicit type argument:<br>
       superInteger = Test.<List<? super
Integer>>pick(superNumber, superInteger);<br>
   }<br>
}</span><br>
<br>
<br>
</div>
<div>Thanks<br>
<br>
--<br>
</div>
<div>Sam<br>
</div>
</div>
</blockquote>
<br>
</body>
</html>