- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Operations on vectors
Sat, 2009-10-10, 23:20
Hi all,
I've checked in the first iteration of immutable vectors and I'm
unsure about a couple of issues regarding integration into the
collections framework.
As of now, there are three operations on immutable vectors that are
(so far) not provided by VectorLike and friends:
def update[B>:A](index: Int, value: B): Vector[B]
def appendFront[B>:A](value: B): Vector[B]
def appendBack[B>:A](value: B): Vector[B]
Question #1 is how these methods should be named. In analogy to
immutable.Map and to prevent people from writing val a = myvector(7) =
9, one could argue that `update' should be called `updated'. However,
this would stand in contrast to `reverse', `drop' and `take'. Method
`appendFront' could be called `::' in analogy to lists, or `+:' in
analogy to buffers (there, however, `+:' is effectful). Method
`appendBack' could be called `+', in analogy to Maps and Sets, but
IIRC earlier discussions, that might lead to ambigous situations with
StringAdd.+ since Vector is covariant in the element type. So, `:+'
would be another option, but IMHO Vector(1,2,3) :+ 4 does not read
particularly nice. One thing one could try is generalizing StringAdd
to arbitrary Iterables, so that one could use 1 + Vector(2,3,4) + 5,
but I'm sure not if that would work (or makes sense after all).
Question #2 is where these methods should be defined. Should they be
defined with a default implementation in terms of builders in (a newly
introduced) immutable.VectorLike? Or should they just apply to the
actual immutable Vector implementation class (currently called
VectorImpl)? The second option seems to require exposing VectorImpl to
client code, while the first option has the side effect of e.g. making
the methods also available on Strings (via WrappedString).
Thoughts? comments?
Thanks,
- Tiark