Red Hat

Fun wildcard-related problem in Java's typesystem

Posted by    |       |    Tagged as

Haha! Try compiling this Java code:

interface Interface<T> {}
class Bang<T> implements Interface<Interface<? super Bang<Bang<T>>>> {

    static void bang() {
        Interface<? super Bang<Byte>> bang = new Bang<Byte>();
    }
        
}

(For me, the compiler stackoverflows, and Eclipse asks for my permission to crash.)

I'm not sure how widely known this problem is. It's not really a bug in the compiler, more like a bug in Java's type system. I found out about it from this excellent paper, which also proposes a solution to the problem, but the paper appears to build on work in this other paper, which I'm also linking because I'm a big fan of Stefan Wehr's work on JavaGI.

UPDATE: Here's another paper dealing with this issue, this time from Microsoft guys.

And, in case you're wondering, yeah, I can make the Ceylon typechecker stackoverflow with the equivalent code:

interface Interface<in T> {}
class Bang<T>() satisfies Interface<Interface<Bang<Bang<T>>>> {}
void bang() {
    Interface<Bang<String>> bang = Bang<String>();
}

I guess ima gunna get right on to implementing the solution proposed in Tate et al.

back to top