Type inference more restrictive in JDK 7 than JDK 6?

前端 未结 1 779
太阳男子
太阳男子 2021-01-02 11:12

I think this might be related to Why does a generic cast of a List to List succeed on Sun JDK 6 but fail to compile on Oracle JDK 7?

相关标签:
1条回答
  • 2021-01-02 12:05

    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.

    0 讨论(0)
提交回复
热议问题