【发布时间】:2017-03-10 22:33:06
【问题描述】:
编译以下代码时出现错误:
import groovy.transform.CompileStatic
@CompileStatic
class Test {
trait BaseTrait {}
trait ExtendingTrait extends BaseTrait {}
static class BaseTraitImplementor implements BaseTrait {}
static class ExtendingTraitImplementor implements ExtendingTrait {}
static class BothTraitImplementor implements ExtendingTrait, BaseTrait {}
static class UsingTraits<T extends BaseTrait> {}
UsingTraits<BaseTraitImplementor> instance1
UsingTraits<ExtendingTraitImplementor> instance2 // <- compiler complains here
UsingTraits<BothTraitImplementor> instance3
}
编译错误:
Groovy-Eclipse: Bound mismatch: The type Test.ExtendingTraitImplementor is not a valid substitute for the bounded parameter <T extends Test.BaseTrait> of the type Test.UsingTraits<T>
看起来编译器无法解析 ExtendingTrait 实际上扩展了 BaseTrait,我需要手动提供它(请参阅 BothTraitImplementor 类)。
我做错了什么还是一个错误?
【问题讨论】:
标签: java generics groovy compiler-errors traits