A bad interaction between self-referential types and bounded wildcards

帅比萌擦擦* 提交于 2019-12-10 10:12:45

问题


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?


回答1:


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".



来源:https://stackoverflow.com/questions/9937422/a-bad-interaction-between-self-referential-types-and-bounded-wildcards

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