Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A bad interaction between self-referential types and bounded wildcards

This case seems to be another one where Eclipse's Java compiler crushes javac. The only question for me is whether it's a bug in JLS or javac.

interface EndoFunctor< C, FC extends EndoFunctor< C, FC > > { /*...*/ }
interface Algebra< C, FC extends EndoFunctor< ? extends C, FC > > { /*...*/ }

The second line compiles in Eclipse, but fails to compile in javac with the message that "type parameter FC is not within its bound".

FC is declared to extend EndoFunctor< ? extends C, FC >, and the bound on FC is that it extend EndoFunctor< D, FC > for the inferred D, which in this case is ? extends C. I think javac doesn't realize that the wildcard represents the same unknown type in both contexts. Eclipse does, though!

Apparently the following gets around the problem in javac:

interface EndoFunctor< C, FC extends EndoFunctor< ? extends C, FC > > { /*...*/ }

but this is a looser definition than I want for that interface.

I could also try

interface Algebra< C, D extends C, FC extends EndoFunctor< D, FC > >

but that approach forces me to carry that extra type parameter D through everywhere.

What to do?

like image 262
Judge Mental Avatar asked Nov 21 '25 12:11

Judge Mental


1 Answers

What to do?

Here are a couple of pragmatic solutions.

  • Try using javac from the latest patch release of Java 7. I recall hearing of certain javac compiler bugs in Java 6 that were only fixed in Java 7 ... but I don't know of a list. (And the Java Bugs Database is hopeless at searching ...)

  • Put up with it, and use one of the two alternatives that you've already found that "work".

like image 136
Stephen C Avatar answered Nov 24 '25 01:11

Stephen C