scala, guidelines on return type - when prefer seq, iterable, traversable

Viewed 9978

When do you choose to type a given function's return type as Seq vs Iterable vs Traversable (or alternatively even deeper within Seq's hierarchy)?

How do you make that decision? We have a lot of code that returns Seqs by default (usually starting from results of a DB query and successive transformations). I tend to want to make the return types Traversable by default and Seq when specifically expecting a given order. But I don't have a strong justification for doing so.

I am perfectly familiar with the definition of each trait, so please don't answer with defining the terms.

5 Answers

Quick note: With Scala 2.13.x, Traversable is out of the picture. Iterable is much more general and it was determined that going forward the duality is not justified. Iterable is now at the top of the collections hierarchy and Traversable has been deprecated.

Related