elm
elm

Reputation: 20415

Scala Iterable and IterableLike differences and uses

What are the differences between Iterable and IterableLike ?

Also what are the appropiate or intended uses of each ?

Namely, how the following methods would differ in the kinds of accepted collections in the arguments ?

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

Upvotes: 3

Views: 373

Answers (1)

lmm
lmm

Reputation: 17431

Iterable just composes together several traits, including IterableLike. I think this is mostly an implementation detail; some methods are put on IterableLike more as a matter of library organization than reflecting any real distinction. I'm not aware of any classes which implement IterableLike and don't implement Iterable (and the converse is impossible because Iterable extends IterableLike); I'd be interested to hear of any. So as far as I know, your methods would behave the same.

I would recommend always using Iterable (unless you're working on the internal implementation of the scala standard library).

Upvotes: 2

Related Questions