Javac type inference issue
Attila Kelemen
attila.kelemen85 at gmail.com
Sun Aug 27 12:49:38 UTC 2023
>
>
> 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,
> it can be approximated by
>
> private static <T> void listMethod(List<? extends List<? super T>> list) {
> }
>
I think you misunderstand Rémi. I know very well what `List<List<? super
T>>` is. And the code snippet I shared is logically correct in the sense
that an infinitely intelligent type inferer could figure out that it is
correct. In fact, if - in the above code - I write `Arrays.<List<? super
T>>asList(args)` instead of `Arrays.asList(args)`, then it compiles fine.
My question is why do I have to explicitly specify the type arguments, why
can't it be inferred.
Btw. If you change the `listMethod` argument to `List<? extends List<?
super T>> list` it won't compile either.
>
> but please, do not use an array of parametrized types List<? super T>[] in
> an API, because creating those are usually unsafe.
>
That is besides the point, I wrote this code to be as simple as possible.
If you need to know, then in the original code I came across this was using
varargs in a safe way (and to avoid further red herrings it was not done
with List).
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/compiler-dev/attachments/20230827/a05a8d71/attachment-0001.htm>
More information about the compiler-dev
mailing list