【发布时间】:2018-02-16 12:40:49
【问题描述】:
考虑这个例子:
trait SpecialIntIterable extends Iterable[Int] {
override def iterator: Iterator[Int] = ??? // implementation not relevant
override def tail : SpecialIntIterable
// plus some more methods...
}
class MyPrimeIterable extends SpecialIntIterable {
override def tail : SpecialIntIterable = ??? // implementation not relevant
}
我正在尝试强制SpecialIntIterable 的所有子类实现tail,因此它再次返回SpecialIntIterable。这可能吗?
目前我收到以下编译器错误:
overriding method tail in trait SpecialIntIterable of type => de.cointrade.trader.SpecialIntIterable;
method tail in trait TraversableLike of type => Iterable[Int] has incompatible type;
(Note that method tail in trait SpecialIntIterable of type => de.cointrade.trader.SpecialIntIterable is abstract,
and is therefore overridden by concrete method tail in trait TraversableLike of type => Iterable[Int])
trait SpecialIntIterable extends Iterable[Int] {
编辑: 我发现的一个解决方案是在SpecialIntIterable 中引入一个抽象辅助方法(例如称为rest)并让tail 委托给它。但是,我想知道如果没有额外的成员是否有可能。
【问题讨论】:
-
...也许我误解了这个问题,因为我不明白“比 what 更具体?”
-
@AndreyTyukin,我的意思比
Iterable[Int]更具体。
标签: scala inheritance overriding