Java will let me do this:
public static class SomeType<I>{}
private static Map<Class<?>, Object> m = new HashMap<Class<?>, Object>();
public static <X> List<SomeType<X>> getList(Class<X> clazz)
{
return (List<SomeType<X>>)m.get(clazz);//warning
}
It will also let me do this:
public static class SomeType<I>{}
private static Map<Class<?>, List<?>> m = new HashMap<Class<?>, List<?>>();
public static <X> List<SomeType<X>> getList(Class<X> clazz)
{
return (List<SomeType<X>>)m.get(clazz);//warning
}
But it won't let me do this:
public static class SomeType<I>{}
private static Map<Class<?>, List<SomeType<?>>> m = new HashMap<Class<?>, List<SomeType<?>>>();
public static <X> List<SomeType<X>> getList(Class<X> clazz)
{
return (List<SomeType<X>>)m.get(clazz);//will not compile
}
unless I resort to the following workaround:
public static class SomeType<I>{}
private static Map<Class<?>, List<SomeType<?>>> m = new HashMap<Class<?>, List<SomeType<?>>>();
public static <X> List<SomeType<X>> getList(Class<X> clazz)
{
return (List<SomeType<X>>)(Object)m.get(clazz);//warning
}
So java makes it possible to explicitly convert from to Object to A<B<C>>
, from A<?>
to A<B<C>>
but not from A<B<?>>
to A<B<C>>
.
why is that?