自引用类型和有界通配符之间的不良交互 [英] A bad interaction between self-referential types and bounded wildcards

查看:163
本文介绍了自引用类型和有界通配符之间的不良交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个案例似乎是Eclipse的Java编译器压缩javac的另一个案例。对我来说唯一的问题是它是否是JLS或javac中的错误。

  interface EndoFunctor< C,FC扩展EndoFunctor< C,FC> > {/*...*/} 
接口代数< C,FC扩展EndoFunctor< ?扩展C,FC> > {/*...*/}

第二行在Eclipse中编译,但无法编译javac,并提示类型参数FC不在其范围内。



声明FC扩展EndoFunctor< ?扩展C,FC>,并且FC上的边界是它扩展了EndoFunctor< D,FC>为推断D,在这种情况下是?扩展C.我认为javac没有意识到通配符在两个上下文中表示相同的未知类型。 Eclipse不会,但!!

显然,以下解决了javac中的问题:

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

但是这是一个比我想要的接口更宽松的定义。

我也可以尝试

  interface Algebra< C,D扩展C,FC扩展EndoFunctor< D,FC> > 

但是这种方法迫使我把这个额外的类型参数D带到任何地方。



该怎么办?

解决方案


该怎么办? / b>

以下是一些实用的解决方案。




    <尝试使用最新的Java 7修补程序版本中的 javac 。我记得在Java 6中只有Java 7中修复了某些javac编译器错误。但我不知道列表。 (并且Java Bugs Database在搜索时没有希望)
  • 使用它,并使用您已经找到的两个替代方案之一即工作。



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?

解决方案

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

这篇关于自引用类型和有界通配符之间的不良交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆