I wonder why the following cast is unchecked:
== Update 1 ==
I know that we have type erasure at runtime. Mind the input parameter is a ArrayList
and not some random list.ArrayList
implements List
and RandomAccess
. I know this cast will not work with LinkedList
or MySpecialArrayList
. but the parameter of this method forbids that. I know (until people remove List
or RandomAccess
from ArrayList
the cast will not fail at runtime, but why is the cast unchecked?
== End update 1 ==
private static <L extends List<GenericTokenType<?>> & RandomAccess> L castArrayList(ArrayList<GenericTokenType<?>> instance) {
return (L) instance;
}
I simplified this to [still warning]
private static <L extends List> L castArrayList(ArrayList instance) {
return (L) instance;
}
and [no warning]
private static List castArrayList(ArrayList instance) {
return (List) instance;
}
Why does this not work. L
is a List
(not the runtime type, but the compiler should get that.
To rephrase the question: Why doesn't it work with a generic parameter return type? Thanks