【发布时间】:2014-03-05 14:06:58
【问题描述】:
我意识到 Java 8 仍处于测试阶段,但这个让我觉得很奇怪:
public class Fields<C extends Enum<C>> {
public Fields(Set<C> columns) {
// A sample column used to find the universe of the enum of Columns.
C sampleCol = columns.iterator().next();
// Java 8 needs a cast here.
Set<C> allColumns = EnumSet.allOf((/*Class<C>)*/ sampleCol.getClass());
// ... there's more to this that I've deleted.
}
}
错误显示:
error: incompatible types: inferred type does not conform to equality constraint(s)
Set<C> allColumns = EnumSet.allOf(sampleCol.getClass());
inferred: C
equality constraints(s): C,CAP#1
where C is a type-variable:
C extends Enum<C> declared in class Test.Fields
where CAP#1 is a fresh type-variable:
CAP#1 extends Enum from capture of ? extends Enum
这是 Java 8 的错误还是新特性?
【问题讨论】:
-
我在 Java 7(51) 和 Java 8(b128) 上进行了尝试,在这两种情况下编译器都需要强制转换,当然在这两种情况下仍然处于警告状态。但是当我删除强制转换时,我在两种情况下都会出错(Java 7,8)。我在 IDEA 13 环境下试过了。