0

I'm using the RingBuffer implementation mentioned in this answer, which inherits from IndexedSeq:

class RingBuffer[A](val index: Int, val data: IndexedSeq[A]) extends IndexedSeq[A] {
  def shiftLeft = new RingBuffer((index + 1) % data.size, data)
  def shiftRight = new RingBuffer((index + data.size - 1) % data.size, data)
  def length = data.length
  def apply(i: Int) = data((index + i) % data.size)
}

What I want to be able to do is this:

var rb = new RingBuffer(0, IndexedSeq(1, 2, 3))
rb = rb.updated(0, 4).shiftLeft

... but updated returns an IndexedSeq.

Should I override updated in the RingBuffer implementation? Or is there a different approach I should take?

Community
  • 1
  • 1
Dylan Nissley
  • 594
  • 1
  • 5
  • 12

1 Answers1

0

I believe you need to have a CanBuildFrom for RingBuffer in implicit scope. If you look at the full signature for updated in the Scaladoc, you'll see where CanBuildFrom is used:

def updated[B >: A, That](index: Int, elem: B)
                         (implicit bf: CanBuildFrom[IndexedSeq[A], B, That])
                         : That

I suggest reading Daniel Sobral's treatise in Scala 2.8 breakOut for more details.

In general, I've found inheriting from most collections-library classes to be a mistake, or at least a lot harder than it would appear.

Community
  • 1
  • 1
Ed Staub
  • 15,480
  • 3
  • 61
  • 91