Why is the compiler not figuring out that i am using the wrong type in the snippet below?
Here's an interface with a generic.
interface Foo<X> {
Map<String, String> getMap();
X addFoos(int foos);
}
Notice the getMap() method which does not use X
.
Why does this compile with just a warning?
void getMapFromFoo(Foo foo) {
Map<Thread, java.util.GregorianCalendar> why = foo.getMap();
}
The any type for the map is accepted with just a warning about unsafe operation.
If however a wildcard is added, the compiler steps back in and only allows the right type.
void getMapFromFooWithWildcard(Foo<?> foo) {
// Ok, as expected
Map<String, String> map = foo.getMap();
// This produces a syntax error, as expected
Map<Thread, java.util.GregorianCalendar> ohWhy = foo.getMap();
}
What is going on?