Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6133211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:11:57+00:00 2026-05-23T17:11:57+00:00

I really like Tie::File , which allows you to tie an array to a

  • 0

I really like Tie::File, which allows you to tie an array to a file’s lines. You can modify the array in any way, and when you’re done with it, you untie it, and the file’s content modifies accordingly.

I’d like to reimplement such behaviour in Scala, and this is what I have so far:

class TiedBuffer(val file:File) extends ArrayBuffer[String] {

  tieFile

  def untie = {
      val writer = new PrintStream(new FileOutputStream(file))
      this.foreach(e => writer.println(e))
      writer.close
      this
  }

  private def tieFile = this ++= scala.io.Source.fromFile(file).getLines()
}

However, the “operators” defined on the ArrayBuffer return various classes, different than my own, for example:

println((new TiedBuffer(somefile) +: "line0").getClass)

gives me a immutable.Vector. I could limit the class to a very small set of predefined methods, but I thought it would be nice if I could offer all of them ( foreach/map/… ).

What should I inherit from, or how should I approach this problem so that I have a fluid array-like interface, which allows me to modify a file’s contents?

BOUNTY: to win the bounty, can you show a working example that makes use of CanBuildFrom to accomplish this task?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:11:58+00:00Added an answer on May 23, 2026 at 5:11 pm

    The methods ending with colon are right associative so in your example you are calling +: of String with a 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’s answer to return TiedBuffer objects instead of ArrayBuffer.

    EDIT2

    Here is an example with CanBuildFrom. You will have to call tie manually though to prevent the file to be tied every time the builder create a new TiedBuffer instance. There is still a lot of room for improvement, for instance ++ 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
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I really like the way gmail has the archive system implemented. I can archive
I really like the way padrino 'just works' with compass but I have a
I really like my Perl code formatted - lines indented, etc. The problem is
I'd really like know if it's possible to do a select statement, which returns
I'm very close to being done, but can't quite figure out how to tie
I really like the way the Drupal form API is conceived. I have never
I really like how in Rails 3 you can ask a scope for its
I tried working with ProtoRPC and I really like how easily I can add
I really like DevX components, but they are pretty expensive, maybe anyone knows free
I really like the fact that Microsoft has taken a commitment to bring MVC

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.