Iterable is one of the basic Scala traits. But it inherently is not immutable. It needs to change its internal state to return one element after another.
Would not it it be better to use an immutable Iterator with next() method which returns pair of values (elem, Iterator) in the same way as the List with head and tail methods is defined? In this case the Iterator could be immutable, reused many times in multi threaded environment and gains of all immutability advantages.
You write “Iterable”, are you thinking of Iterator?
Yes. Iterable as the second main trait in the Scala collections after Traversable and the Iterator as something mutable.
I would say the term “iterator” is already taken since a long time: Scala’s
Iterator does the same as the Iterator in Java and C++.
The suffix “-tor” means something that does something, so arguably, it
should be something mutable.
But what you want exists too, as Iterable.head and Iterable.tail.
Are Iterable.head and Iterable.tail implemented as immutable, so two consequence calls Iterable.head return the same results?