【发布时间】:2018-01-24 19:20:51
【问题描述】:
我目前在这一行遇到编译错误:
chosenState = chosenState.withProperty(property, value);
其中property 是IProperty<?>,值是Comparable<?>。 withProperty的签名是:
<T extends Comparable<T>, V extends T> IBlockState withProperty(IProperty<T> property, V value);
IProperty 的类型参数是:
IProperty<T extends Comparable<T>>
编译错误是:
chosenState = chosenState.withProperty(property, value);
^
required: IProperty<T>,V
found: IProperty<CAP#1>,Comparable<CAP#2>
reason: inference variable T has incompatible bounds
equality constraints: CAP#1
lower bounds: V,Comparable<CAP#2>
where T,V are type-variables:
T extends Comparable<T> declared in method <T,V>withProperty(IProperty<T>,V)
V extends T declared in method <T,V>withProperty(IProperty<T>,V)
where CAP#1,CAP#2 are fresh type-variables:
CAP#1 extends Comparable<CAP#1> from capture of ?
CAP#2 extends Object from capture of ?
我想通过查找类型 T 和 V 来解决它,其中:
-
IProperty<?>扩展IProperty<T> -
Comparable<?>扩展V扩展T扩展Comparable<T>
一些可以工作的类型参数是<?, ?>,但是?s 都是未知的但类型相同,或者至少第一个? 扩展了第二个。有没有办法告诉编译器?
我知道这两个?是一样的,因为value是IProperty<T extends Comparable<T>>中Collection<T> getAllowedValues();方法返回的集合的一个元素。
我知道我可以通过转换为 IProperty 和 Comparable 来解决这个问题,但我想避免使用原始类型。
【问题讨论】:
-
我不明白你想做什么。请展示实现。
-
什么是
property和value,在演员表之前? -
看起来像X/Y problem。你到底想做什么?
-
@Onheiron:对不起,我在复制行时忘记删除了。这就是我现在正在做的一种解决方法,但我希望它能够工作,而不必将它们转换为原始类型。
-
如何确定
Comparable<?>是IProperty<?>的扩展?
标签: java generics compiler-errors