Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic way to refer to "list" in Scala?

Tags:

scala

How can I refer to ArrayBuffer and Vector in a more generic way?

For example - one of my functions takes a Vector as an argument, while another returns an ArrayBuffer. What is a common "iterface" that I can use?

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

like image 648
Andriy Drozdyuk Avatar asked Dec 17 '22 17:12

Andriy Drozdyuk


2 Answers

See here for an overview of the inheritance relationship between the collections classes.

You'll see that IndexedSeq is a common trait for both ArrayBuffer and Vector.

EDIT: IndexedSeq vs. Seq:

From the 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 just as well use Seq, as the implementations will be provided by ArrayBuffer and Vector in any case.

like image 110
Knut Arne Vedaa Avatar answered Jan 12 '23 15:01

Knut Arne Vedaa


I would use SeqLike or more generic TraversableOnce which would also apply for Maps.

like image 28
Thomas Rawyler Avatar answered Jan 12 '23 15:01

Thomas Rawyler