首页 > 解决方案 > 扩展特征不匹配的 Groovy 泛型类型 - 不是有界参数的有效替代品

问题描述

下面的代码不能在 Groovy 3.0.4 中编译(尽管它在 Groovy 2.5.0 及更低版本中很好)

Factory<SomeImpl> f = new Factory<>();

trait Base {}
class SomeImpl implements Base {}
class Factory<T extends Base> {}

我得到的确切错误是:

The type SomeImpl is not a valid substitute for the bounded parameter <T extends Base> @ line 1, column 9.
Factory<SomeImpl> f = new Factory<>();

将Basetrait更改为interface允许按预期编译代码并且它可以工作(就像在 Java 中一样)。

trait 是否与不允许我们进行此类构造的接口有所不同?还是最近的 groovy 版本中的错误?

标签: genericsgroovytraits

解决方案


推荐阅读