JDK8 type inference issue

浪尽此生 提交于 2019-12-05 17:10:32
Holger

Whenever you create a generic method with a signature that promises to return whatever the caller wishes, you are asking for trouble. You should have got an “unchecked” warning from the compiler which basically means: unexpected ClassCastExceptions may occur.

You expect the compiler to infer

List<CB> bl1 = Arrays.asList(YourClass.<CA,CB>convert(a));

whereas the compiler actually inferred

List<CB> bl1 = Arrays.asList(YourClass.<CA,CB[]>convert(a));

as far as I know, because it prefers method invocations not requiring a varargs packaging (which is compatible with pre-varargs code).

This fails because your convert method does not return the expected array type.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!