Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 919
elm Avatar asked Mar 12 '26 07:03

elm


1 Answers

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).

like image 67
lmm Avatar answered Mar 15 '26 04:03

lmm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!