【问题标题】:Scala Iterable and IterableLike differences and usesScala Iterable 和 IterableLike 的区别和使用
【发布时间】:2014-11-21 10:29:08
【问题描述】:

IterableIterableLike 有什么区别?

还有什么是合适的预期的用途?

即,以下方法在参数中接受的集合种类方面有何不同?

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 标准库的内部实现)。

【讨论】:

    猜你喜欢
    • 2016-08-20
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-17
    • 2013-06-16
    相关资源
    最近更新 更多