Generic ype inference with lower bounded types
Sam Munkes
sam.munkes at gmail.com
Sat Jan 3 00:07:32 UTC 2015
Hi Guys,
A colleague of mine recently posted the following question to
StackOverflow:
http://stackoverflow.com/questions/27652867/java-type-inference-with-lower-bounded-types
Does anyone on the list have insight into why the compiler does not infer
lower bounded types?
static class Test {
static <T> T pick(T one, T two) {
return two;
}
static void testUpperBound() {
List<? extends Integer> extendsInteger = new ArrayList<>();
// List<? extends Integer> is treated as a subclass of List<?
extends Number>
List<? extends Number> extendsNumber = extendsInteger;
// List<? extends Number> is inferred as the common superclass
extendsNumber = pick(extendsInteger, extendsNumber);
}
static void testLowerBound() {
List<? super Number> superNumber = new ArrayList<>();
// List<? super Number> is treated as a subclass of List<? super
Integer>
List<? super Integer> superInteger = superNumber;
// The inferred common type should be List<? super Integer>,
// but instead we get a compile error:
*superInteger = pick(superNumber, superInteger);*
// It only compiles with an explicit type argument:
superInteger = Test.<List<? super Integer>>pick(superNumber,
superInteger);
}
}
Thanks
--
Sam
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20150102/5347defc/attachment.html>
More information about the compiler-dev
mailing list