This page is no longer maintained — Please continue to the home page at www.scala-lang.org

writing my own ArrayBuffer

1 reply
Jonathan Bachrach
Joined: 2012-02-18,
User offline. Last seen 42 years 45 weeks ago.
i'm looking to created ArrayBuffer like functionality that i can add to a class (as I can't inherit from ArrayBuffer and my own superclass).  here's my attempt:
class Buf[A](val data: ArrayBuffer[A] = ArrayBuffer[A]()) extends BufferLike[A, Buf[A]] {   override def foreach[U](f: A => U) = data.foreach(f)  def iterator = data.iterator;  def newBuilder = new ArrayBuffer[A] mapResult (new Buf(_));  def apply(idx: Int): A = data(idx);   def update(idx: Int, elem: A) = data.update(idx, elem);  def length: Int = data.length  def clear() = data.clear()  def +=(elem: A): this.type = { data += elem; this }  def +=:(elem: A): this.type = { /* data +=: elem; */ this }   def insertAll(n: Int, iter: Traversable[A]) = data.insertAll(n, iter)  def remove(n: Int): A = data.remove(n)}
unfortunately, i get the following error which i don't understand how to fix:
[error] /Users/jrb/Documents/bar/transactors/sbt/process/src/main/scala/buf.scala:9: type arguments [A,Process.Buf[A]] do not conform to trait BufferLike's type parameter bounds [A,+This <: scala.collection.mutable.BufferLike[A,This] with scala.collection.mutable.Buffer[A]] [error] class Buf[A](val data: ArrayBuffer[A] = ArrayBuffer[A]()) extends Node with BufferLike[A, Buf[A]] {[error]                                                                             ^[error] one error found
is there a better way to do this sort of forwarding of the collection protocol to an underlying ArrayBuffer?  
A working code snippet would be great appreciated!
Tim Underwood
Joined: 2010-10-22,
User offline. Last seen 42 years 45 weeks ago.
Re: writing my own ArrayBuffer
BufferProxy might be what you want:

import scala.collection.mutable.{ArrayBuffer, BufferProxy}

class Buf[A](val self: ArrayBuffer[A] = ArrayBuffer[A]()) extends BufferProxy[A] {
  override def apply(idx: Int): A = {
    println("apply("+idx+")")
    super.apply(idx)
  }
}

The error you are seeing with your code means you also need to extend Buffer:

import scala.collection.mutable.{ArrayBuffer, Buffer, BufferLike}

class Buf[A](val data: ArrayBuffer[A] = ArrayBuffer[A]()) extends Buffer[A] with BufferLike[A, Buf[A]] {
  override def foreach[U](f: A => U) = data.foreach(f)
  def iterator = data.iterator
  override def newBuilder = new ArrayBuffer[A] mapResult (new Buf(_))
  def apply(idx: Int): A = data(idx)
  def update(idx: Int, elem: A) = data.update(idx, elem);
  def length: Int = data.length
  def clear() = data.clear()
  def +=(elem: A): this.type = { data += elem; this }
  def +=:(elem: A): this.type = { /* data +=: elem; */ this }
  def insertAll(n: Int, iter: Traversable[A]) = data.insertAll(n, iter)
  def remove(n: Int): A = data.remove(n)
}

-Tim

On Sat, Feb 18, 2012 at 9:55 AM, Jonathan Bachrach <jackbackrack [at] gmail [dot] com> wrote:
i'm looking to created ArrayBuffer like functionality that i can add to a class (as I can't inherit from ArrayBuffer and my own superclass).  here's my attempt:
class Buf[A](val data: ArrayBuffer[A] = ArrayBuffer[A]()) extends BufferLike[A, Buf[A]] {   override def foreach[U](f: A => U) = data.foreach(f)  def iterator = data.iterator;  def newBuilder = new ArrayBuffer[A] mapResult (new Buf(_));  def apply(idx: Int): A = data(idx);   def update(idx: Int, elem: A) = data.update(idx, elem);  def length: Int = data.length  def clear() = data.clear()  def +=(elem: A): this.type = { data += elem; this }  def +=:(elem: A): this.type = { /* data +=: elem; */ this }   def insertAll(n: Int, iter: Traversable[A]) = data.insertAll(n, iter)  def remove(n: Int): A = data.remove(n)}
unfortunately, i get the following error which i don't understand how to fix:
[error] /Users/jrb/Documents/bar/transactors/sbt/process/src/main/scala/buf.scala:9: type arguments [A,Process.Buf[A]] do not conform to trait BufferLike's type parameter bounds [A,+This <: scala.collection.mutable.BufferLike[A,This] with scala.collection.mutable.Buffer[A]] [error] class Buf[A](val data: ArrayBuffer[A] = ArrayBuffer[A]()) extends Node with BufferLike[A, Buf[A]] {[error]                                                                             ^[error] one error found
is there a better way to do this sort of forwarding of the collection protocol to an underlying ArrayBuffer?  
A working code snippet would be great appreciated!

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland