开发者

Why does this compile? The code seems to be breaking constraints on the type parameters

开发者 https://www.devze.com 2023-02-02 04:14 出处:网络
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

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开发者_如何学Python 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


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消