【发布时间】:2010-01-20 10:45:41
【问题描述】:
在 Scala 中,如何将容器特征(如 Traversable[Content])添加到另一个扩展容器的特征(因此对其内容的可见性有限?
例如,下面的代码尝试为需要 Traversable 的容器定义一个 trait WithIter(当然,实际上我在 Container 中还有其他东西)。
import scala.collection._
trait Container {
type Value
}
trait WithIter extends Container with immutable.Traversable[Container#Value]
class Instance extends WithIter {
type Value = Int
def foreach[U](f : (Value) => (U)) : Unit = {}
}
编译器(scalac 2.8.0.Beta1-RC8)发现错误:
错误:类 Instance 需要是抽象的,因为 [U](f: (Container#Value) => U)Unit 类型的特征 GenericTraversableTemplate 中的方法 foreach 未定义
有什么简单的方法吗?
【问题讨论】:
标签: scala