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

后端 未结 5 1101
天命终不由人
天命终不由人 2021-01-31 07:44

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

5条回答
  •  攒了一身酷
    2021-01-31 08:03

    • Use Seq by default everywhere.
    • Use IndexedSeq when you need to access by index.
    • Use anything else only in special circumstances.

    These are the "common-sense" guidelines. They are simple, practical, and work well in practice while balancing principles and performance. The principles are:

    1. Use a type that reflects how the data is organized (thanks OP and ziggystar).
    2. Use interface types in both method arguments and return types. Both inputs and return types of an API benefit from the flexibility of generality.

    Seq satisfies both principles. As described in http://docs.scala-lang.org/overviews/collections/seqs.html:

    A sequence is a kind of iterable that has a [finite] length and whose elements have fixed index positions, starting from 0.

    90% of the time, your data is a Seq.

    Other notes:

    • List is an implementation type, so you shouldn't use it in an API. A Vector for instance can't be used as a List without going through a conversion.
    • Iterable doesn't define length. Iterable abstracts across finite sequences and potentially infinite streams. Most of the time one is dealing with finite sequences so you "have a length," and Seq reflects that. Frequently you won't actually make use of length. But it's needed often enough, and is easy to provide, so use Seq.

    Drawbacks:

    There are some slight downsides to these "common-sense" conventions.

    • You can't use List cons pattern matching i.e. case head :: tail => ... . You can use :+ and +: as described here. Importantly, however, matching on Nil still works as described in Scala: Pattern matching Seq[Nothing].

    Footnotes:

    • I'm not discussing Map here because the question, sensibly, doesn't ask about it.
    • I'm only addressing immutable collections here.
    • The guidelines I suggest are consistent with Should I use List[A] or Seq[A] or something else?

提交回复
热议问题