Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I use GenSeq by default?

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

like image 545
qertoip Avatar asked Jan 27 '13 18:01

qertoip


1 Answers

No. The most generic collection types that should appear in APIs are:

  • Seq — sequential collection
  • Set — mathematical sets (no duplicated values)
  • Map — associations / associative sets (conceptually the same as discrete PartialFunctions)

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

like image 177
Randall Schulz Avatar answered Oct 22 '22 02:10

Randall Schulz