Is this a Java generic type inference compiler bug? [duplicate]

北城以北 提交于 2021-01-27 17:38:37

问题


I am thinking about constrained type inference at the call site of a function. In the following snippet I get a compile error for the first inference call in the main function as expected (String is not a subtype of X so we should not be able to infer this type) but I do not get a compile error when I replace the abstract class with an interface and do the same thing even though clearly String is not a subtype of Y. This must be a compiler bug right? (I have tried with jdk 1.8.121 + 1.8.161).

public class Why
{
    static abstract class X
    {
    }

    static <Z extends X> Z infer1()
    {
        return null;
    }

    static interface Y
    {
    }

    static <Z extends Y> Z infer2()
    {
        return null;
    }

    public static void main(String[] args)
    {
        String x = infer1();
        String y = infer2();
    }
}

来源:https://stackoverflow.com/questions/56044697/is-this-a-java-generic-type-inference-compiler-bug

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