Type inference more restrictive in JDK 7 than JDK 6?

情到浓时终转凉″ 提交于 2019-11-30 13:39:56

Strictly according to the spec, T cannot be inferred (per 15.12.2.7), so it should be taken as Object.

This can be viewed as a failure of the spec. This is how spec infers R: first there is constraint R :> BigDecimal, where :> means is a supertype of. The inference rules then choose R=BigDecimal since it's the most specific type satisfying the constraint.

Now, since T:>R, T:>BigDecimal, one would think this should yield T=BigDecimal too.

Unfortunately the inference rules do not take T:>R into account. There is no contraint on T. T is not inferred through the same principle.

While it sucks, spec is spec. Your code should not compile. Javac6 is wrong there.

In Java 8 there's an great improvement on inference rules to make lambda expression easier to use. Hopefully your code should compile in Java 8.

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