Type inference: bug or feature?

Justin Dekeyser justin.dekeyser at gmail.com
Mon Jul 27 08:44:43 UTC 2020


Hello,

Again, thanks for the prompt response!

Well, I'm not really sure about the answer but, to my understanding, if
this:
List<Integer> x = emptyList(Number.class);
compiles correctly (thus U inferred as Number, V inferred as Integer as
expected),
then why this
List<Integer> x = (List<Integer>) emptyList(Number.class);
does not compile ? (with explicit or var-typing)

On the other hand, the following code
var x = (Function<String,String>) x -> x;
compile with no error, hence in this case, casting does help in type
inference.

I really feel that, although a bit "over complicated", there is something
that I either really don't get, or something else :/

Regards,

Justin Dekeyser



On Mon, Jul 27, 2020 at 10:21 AM Florian Weimer <fweimer at redhat.com> wrote:

> * Justin Dekeyser:
>
> > Nevertheless, the point was not really about list stuffs, and is not
> > related to the lack of co/contra variance.
>
> > You may replace List with any other generic class Foo<T> and Integer
> > and Number with any other class satisfying the same inheritance
> > relations.
>
> Ahh, well, but why are such casts useful?  The results will be wrong in
> pretty much every case.  Their correctness cannot be checked at run
> time, either.
>
> Thanks,
> Florian
>
>


More information about the core-libs-dev mailing list