Is it the best practice to use GenSeq as a "default" collection type? It seems to be the most generic collection interface. However I don't see it widely used in code examples (the more specific Seq is used instead).
No. The most generic collection types that should appear in APIs are:
Seq
— sequential collectionSet
— mathematical sets (no duplicated values)Map
— associations / associative sets (conceptually the same as discrete PartialFunction
s)The only abstractions that are neutral to the distinction between the foregoing are Traversable
and Iterable
. (E.g. Map[K, V]
is both Iterable[(K, V)]
and Traversable[(K, V)]
.
If all that matters for an actual parameter is that its elements may be examined in some (unspecified) order, then Traversable
captures that characteristic.
Class or trait names that include "Like" or "Once" or "Gen" are part of the internal magic that makes Scala collections exhibit the "principle of least surprise" (chief among them being the property that invoking a HOF upon them such as map
or filter
yields a result with a matching concrete type as that of the collection upon which that HOF was invoked).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With