<html><body><div style="font-family: arial, helvetica, sans-serif; font-size: 12pt; color: #000000"><div><br></div><div><br></div><hr id="zwchr" data-marker="__DIVIDER__"><div data-marker="__HEADERS__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><b>From: </b>"attila kelemen85" <attila.kelemen85@gmail.com><br><b>To: </b>"compiler-dev" <compiler-dev@openjdk.org><br><b>Sent: </b>Sunday, August 27, 2023 2:25:39 PM<br><b>Subject: </b>Javac type inference issue<br></blockquote></div><div data-marker="__QUOTED_TEXT__"><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr">Hi,<br><div>I'm never sure what type inference is supposed to work with or not, but I have an example what I find strange that it does not compile successfully:</div><br><div>```</div><div>import java.util.*;<br><br>public class Test {<br> private static <T> void arrayMethod(List<? super T>[] args) {<br> listMethod(Arrays.asList(args));<br> }<br><br> private static <T> void listMethod(List<List<? super T>> list) {<br> }<br>}<br></div><div>```</div><br><div>I have tried this with 11.0.20, 20.0.2 and 21 RC, and all fails with this:</div><br><div>> listMethod(Arrays.asList(args));<br>> ^<br>> required: List<List<? super T#1>><br>> found: List<List<? super T#2>><br>> reason: inference variable T#3 has incompatible bounds<br>> equality constraints: List<? super CAP#1><br>> lower bounds: List<? super T#2><br>> where T#1,T#2,T#3 are type-variables:<br>> T#1 extends Object declared in method <T#1>listMethod(List<List<? super T#1>>)<br>> T#2 extends Object declared in method <T#2>arrayMethod(List<? super T#2>[])<br>> T#3 extends Object declared in method <T#3>asList(T#3...)<br>> where CAP#1 is a fresh type-variable:<br>> CAP#1 extends Object super: T#2 from capture of ? super T#2<br></div><br><div>Of course, if I explicitly specify the type argument for `Arrays.asList`, then it compiles fine, but I fail to see why this should not work without explicitly specifying the type arguments.</div></div></blockquote><div><br></div><div>It's not a bug, it's how the capture works, a List<List<? super T>> is a List where each List can have a different type, but here you want a List of List with each list with the same type,<br data-mce-bogus="1"></div><div>it can be approximated by<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><div>private static <T> void listMethod(List<? extends List<? super T>> list) {<br>}</div><div><br data-mce-bogus="1"></div><div>but please, do not use an array of parametrized types List<? super T>[] in an API, because creating those are usually unsafe.<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div><blockquote style="border-left:2px solid #1010FF;margin-left:5px;padding-left:5px;color:#000;font-weight:normal;font-style:normal;text-decoration:none;font-family:Helvetica,Arial,sans-serif;font-size:12pt;"><div dir="ltr"><br><div>Thanks,</div><div>Attila</div></div></blockquote><div><br></div><div>regards,<br data-mce-bogus="1"></div><div>RĂ©mi<br data-mce-bogus="1"></div><div><br data-mce-bogus="1"></div></div></div></body></html>