A common way to reference a "list" in Scala?

How can I reference ArrayBuffer and Vector in a more general way?

For example, one of my functions takes a vector as an argument, and the other returns an ArrayBuffer. What is a regular β€œinterface” that I can use?

For example, in Java, I could use the List or Collection interface to pass them.

+4
source share
2 answers

See here for an overview of inheritance relationships between collection classes.

You will see that IndexedSeq is a common feature for both ArrayBuffer and Vector.

EDIT: IndexedSeq vs Seq:

From doc: Indexed sequences do not add any new methods wrt Seq, but promise efficient implementations of random access patterns. This means that in this context, you could also use Seq, since implementations would be provided by ArrayBuffer and Vector anyway.

+9
source

I would use SeqLike or the more general TraversableOnce , which would also apply for Map s.

+3
source

Source: https://habr.com/ru/post/1339523/


All Articles