4

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.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Andriy Drozdyuk
  • 58,435
  • 50
  • 171
  • 272

2 Answers2

9

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.

Knut Arne Vedaa
  • 15,372
  • 11
  • 48
  • 59
  • Why not just "Seq"? I mean how do you decide? In java i only had a limited number of choices... – Andriy Drozdyuk Feb 13 '11 at 17:28
  • Ah, the curse of unlimited choices...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. – Knut Arne Vedaa Feb 13 '11 at 19:46
  • Could you amend your answer with this comment so that I can accept it? Thanks – Andriy Drozdyuk Feb 14 '11 at 03:09
3

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

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Thomas Rawyler
  • 1,075
  • 7
  • 16
  • 4
    Why not just use Traversable or Seq? – gruenewa Feb 13 '11 at 09:18
  • You're right, that would be sufficient for Vector and ArrayBuffer (+1) – Thomas Rawyler Feb 13 '11 at 09:24
  • 2
    `SeqLike` isn't much use in this case, as you still have the supply the actual representation as a type param. `Seq` is the idiomatic choice here, it's less restrictive than `Traversable` and has implementations of some methods that perform better than Traversable's – Kevin Wright Feb 13 '11 at 09:40