Methods ending with a colon are right associative , so in your example you call +: from String with the TiedBuffer as parameter. If you want to test +: from ArrayBuffer , you can do:
println((new TiedBuffer(somefile).+:("line0")).getClass)
or
println(("line0" +: new TiedBuffer(somefile)).getClass)
EDIT
I missed the point in your question, see John answer to return TiedBuffer objects instead of ArrayBuffer .
EDIT2
Here is an example with CanBuildFrom . You will have to call tie manually, but to prevent the file from being bound every time the creator creates a new instance of TiedBuffer . There is still much room for improvement, for example ++ will not work, but it should get you started.
import collection.generic.CanBuildFrom import collection.mutable._ import java.io.{PrintStream, FileOutputStream, File} class TiedBuffer(val file: File) extends ArrayBuffer[String] with BufferLike[String, TiedBuffer] with IndexedSeqOptimized[String, TiedBuffer] { def tie = { clear this ++= scala.io.Source.fromFile(file).getLines() } def untie = { val writer = new PrintStream(new FileOutputStream(file)) this.foreach(e => writer.println(e)) writer.close this } override def newBuilder: Builder[String, TiedBuffer] = new ArrayBuffer mapResult { x: Seq[String] => (new TiedBuffer(file) ++= x) } } object TiedBuffer { implicit def canBuildFrom: CanBuildFrom[TiedBuffer, String, TiedBuffer] = new CanBuildFrom[TiedBuffer, String, TiedBuffer] { def apply(): Builder[String, TiedBuffer] = throw new RuntimeException("Cannot create a new TiedBuffer from scratch") def apply(from: TiedBuffer): Builder[String, TiedBuffer] = from.newBuilder } }
source share