为什么会这样编译?该代码似乎打破了对类型参数的限制

发布于 2024-10-10 15:36:26 字数 1466 浏览 2 评论 0原文

在下面的测试中,TesterClass对其两个类型参数之间的关系施加了约束。方法 func2() 似乎打破了这个约束,我希望它会在某个地方导致键入编译错误(在 func2 的定义上,或者每当该类与除 String 之外的任何第二个参数一起使用时),但事实并非如此!

此外,如果我调用 func2 并将结果保存在适当类型的变量中,编译将失败(根据该变量的类型)。但是,执行相同的操作并保存为更通用的类型(例如 Object)会成功,尽管函数的返回类型在两种情况下(在向上转换之前)应该具有相同的类型。

这是怎么回事?

谢谢!

public class TestGenerics {
    public static class ParamedType<T> {}


    public class TesterClass<A extends ParamedType<B>, B> {
        public TesterClass<A, B> func() {
            return new TesterClass<A, B>();
        }

        public TesterClass<A, String> func2() {
            return new TesterClass<A, String>();
        }
    }

    public Object test() {
        // How can I use these type parameters? Doesn't .func2 now have an invalid return type?
        TesterClass<ParamedType<Integer>,Integer> testClass = new TesterClass<TestGenerics.ParamedType<Integer>, Integer>();

        //TesterClass<ParamedType<String>, Integer> res2 = testClass.func2(); // <-- will not compile
        Object res = testClass.func2(); // Compiles
        return res;
    }
}

编辑:此不能在 javac 中编译(下面报告的版本)。我正在使用 Eclipse,并试图找出实际运行的编译器是什么。会更新。可能是 JDT(Eclipse 编译器)错误。

我已经打开了 eclipse 的 jdt 的错误报告: https://bugs.eclipse.org/bugs/show_bug.cgi?id=333503

In the following test, TesterClass places a constraint on the relation between its two type parameters. The method func2() seems to break that constraint, and I expect it to cause a typing compilation error somewhere (on func2's definition, or whenever the class is used with any second parameter other than String), but it doesn't!

Furthermore, if I call func2 and save the result in an appropriately typed variable, the compilation fails (on the type of that variable). But doing the same and saving in a more general type (such as Object) succeeds, despite the fact the the function's return type should have the same type in both cases (before the up-cast).

What's going on here?

Thanks!

public class TestGenerics {
    public static class ParamedType<T> {}


    public class TesterClass<A extends ParamedType<B>, B> {
        public TesterClass<A, B> func() {
            return new TesterClass<A, B>();
        }

        public TesterClass<A, String> func2() {
            return new TesterClass<A, String>();
        }
    }

    public Object test() {
        // How can I use these type parameters? Doesn't .func2 now have an invalid return type?
        TesterClass<ParamedType<Integer>,Integer> testClass = new TesterClass<TestGenerics.ParamedType<Integer>, Integer>();

        //TesterClass<ParamedType<String>, Integer> res2 = testClass.func2(); // <-- will not compile
        Object res = testClass.func2(); // Compiles
        return res;
    }
}

EDIT: This doesn't compile in javac (versions reported below). I'm using Eclipse, and trying to find out what's the compiler that's actually running. Will update. May be a JDT (Eclipse compiler) bug.

I've opened a bug report for eclipse's jdt:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=333503

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傻比既视感 2024-10-17 15:36:27

简单的答案:它不能编译,至少在 javac 1.7 下:

Test.java:10: type parameter A is not within its bound
    public TesterClass<A, String> func2() {
                       ^
  where A,B are type-variables:
    A extends ParamedType<B> declared in class Test.TesterClass
    B extends Object declared in class Test.TesterClass
Test.java:11: type parameter A is not within its bound
        return new TesterClass<A, String>();
                               ^
  where A,B are type-variables:
    A extends ParamedType<B> declared in class Test.TesterClass
    B extends Object declared in class Test.TesterClass
2 errors

你没有说你在什么下编译它 - 我的猜测是你的 Java 编译器有一个错误。

Simple answer: it doesn't compile, at least under javac 1.7:

Test.java:10: type parameter A is not within its bound
    public TesterClass<A, String> func2() {
                       ^
  where A,B are type-variables:
    A extends ParamedType<B> declared in class Test.TesterClass
    B extends Object declared in class Test.TesterClass
Test.java:11: type parameter A is not within its bound
        return new TesterClass<A, String>();
                               ^
  where A,B are type-variables:
    A extends ParamedType<B> declared in class Test.TesterClass
    B extends Object declared in class Test.TesterClass
2 errors

You didn't say what you were compiling it under - my guess is that your Java compiler has a bug in it.

傲性难收 2024-10-17 15:36:27

显然,这是 JDT.core 中的 Eclipse bug。我已在 https://bugs.eclipse.org/bugs 打开错误报告/show_bug.cgi?id=333503

Apparently, this is an Eclipse bug in JDT.core. I've opened a bug report at https://bugs.eclipse.org/bugs/show_bug.cgi?id=333503

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文