【问题标题】:Scala Iterable and IterableLike differences and usesScala Iterable 和 IterableLike 的区别和使用
【发布时间】:2014-11-21 10:29:08
【问题描述】:
Iterable 和 IterableLike 有什么区别?
还有什么是合适的或预期的用途?
即,以下方法在参数中接受的集合种类方面有何不同?
def f[A](c: Iterable[A]) = c.foreach(print)
f: [A](c: Iterable[A])Unit
def g[A,B](c: IterableLike[A,B]) = c.foreach(print)
g: [A, B](c: scala.collection.IterableLike[A,B])Unit
【问题讨论】:
标签:
scala
scala-collections
【解决方案1】:
Iterable 只是将几个特征组合在一起,包括IterableLike。我认为这主要是一个实现细节; IterableLike 上的一些方法更多地是作为图书馆组织的问题,而不是反映任何真正的区别。我不知道有任何类实现了IterableLike 而没有实现Iterable(反过来是不可能的,因为Iterable 扩展了IterableLike);我很想听听。据我所知,你的方法的行为是一样的。
我建议始终使用Iterable(除非您正在处理 scala 标准库的内部实现)。