views:

875

answers:

4

I have seen many examples of ARM (automatic resource management) on the web for Scala. It seems to be a rite-of-passage to write one, though most look pretty much like one another. I did see a pretty cool example using continuations, though.

At any rate, a lot of that code has flaws of one type or another, so I figured it would be a good idea to have a reference here on Stack Overflow, where we can vote up the most correct and appropriate versions.

+4  A: 

Daniel, good you asked this. I am myself intrigued after seeing James Iry's code. I see a gradual 4 step evolution for doing ARM in Scala:

  1. No ARM: Dirt
  2. Only closures: Better, but multiple nested blocks
  3. Continuation Monad: Use For to flatten the nesting, but unnatural separation in 2 blocks
  4. Direct style continuations: Nirava, aha! This is also the most type-safe alternative: a resource outside withResource block will be type error.

What I would really love to see is a presentation describing these. It will be very educational and should convince the begots that there is a world beyond Monads :)

Mushtaq Ahmed
Mind you, CPS in Scala are implemented through monads. :-)
Daniel
Mushtaq,3) You can do resource management in a monad that isn't the monad of continuation4) Resource management using my withResources/resource delimited continuations code is no more (and no less) type safe than "using." It is still possible to forget to manage a resource that needs it.compare using(new Resource()) { first => val second = new Resource() //oops! // use resources } // only first gets closed withResources { val first = resource(new Resource()) val second = new Resource() // oops! // use resources... } // only first gets closed
James Iry
Daniel, CPS in Scala is like CPS in any functional language. It's delimited continuations that use a monad.
James Iry
James, thanks for explaining it well. Sitting in India I could only wish I was there for your BASE talk. Waiting to see when you put those slides online :)
Mushtaq Ahmed
+11  A: 

Chris Hansen's blog entry 'ARM Blocks in Scala: Revisited' from 3/26/09 talks about about slide 21 of Martin Odersky's FOSDEM presentation. This next block is taken straight from slide 21 (with permission):

def using[T <: { def close() }]
    (resource: T)
    (block: T => Unit) 
{
  try {
    block(resource)
  } finally {
    if (resource != null) resource.close()
  }
}

--end quote--

Then we can call like this:

using(new BufferedReader(new FileReader("file"))) { r =>
  var count = 0
  while (r.readLine != null) count += 1
  println(count)
}

What are the drawbacks of this approach? That pattern would seem to address 95% of where I would need automatic resource management...

Edit: added code snippet


Edit2: extending the design pattern - taking inspiration from python with statement and addressing:

  • statements to run before the block
  • re-throwing exception depending on the managed resource
  • handling two resources with one single using statement
  • resource-specific handling by providing an implicit conversion and a Managed class

This is with Scala 2.8.

trait Managed[T] {
  def onEnter(): T
  def onExit(t:Throwable = null): Unit
  def attempt(block: => Unit): Unit = {
    try { block } finally {}
  }
}

def using[T <: Any](managed: Managed[T])(block: T => Unit) {
  val resource = managed.onEnter()
  var exception = false
  try { block(resource) } catch  {
    case t:Throwable => exception = true; managed.onExit(t)
  } finally {
    if (!exception) managed.onExit()
  }
}

def using[T <: Any, U <: Any]
    (managed1: Managed[T], managed2: Managed[U])
    (block: T => U => Unit) {
  using[T](managed1) { r =>
    using[U](managed2) { s => block(r)(s) }
  }
}

class ManagedOS(out:OutputStream) extends Managed[OutputStream] {
  def onEnter(): OutputStream = out
  def onExit(t:Throwable = null): Unit = {
    attempt(out.close())
    if (t != null) throw t
  }
}
class ManagedIS(in:InputStream) extends Managed[InputStream] {
  def onEnter(): InputStream = in
  def onExit(t:Throwable = null): Unit = {
    attempt(in.close())
    if (t != null) throw t
  }
}

implicit def os2managed(out:OutputStream): Managed[OutputStream] = {
  return new ManagedOS(out)
}
implicit def is2managed(in:InputStream): Managed[InputStream] = {
  return new ManagedIS(in)
}

def main(args:Array[String]): Unit = {
  using(new FileInputStream("foo.txt"), new FileOutputStream("bar.txt")) { 
    in => out =>
    Iterator continually { in.read() } takeWhile( _ != -1) foreach { 
      out.write(_) 
    }
  }
}
huynhjl
There are alternatives, but I don't meant to imply there's something wrong with that. I just want all those answers here, on Stack Overflow. :-)
Daniel
+7  A: 

Here's James Iry solution using continuations:

// standard using block definition
def using[X <: {def close()}, A](resource : X)(f : X => A) = {
   try {
     f(resource)
   } finally {
     resource.close()
   }
}

// A DC version of 'using' 
def resource[X <: {def close()}, B](res : X) = shift(using[X, B](res))

// some sugar for reset
def withResources[A, C](x : => A @cps[A, C]) = reset{x}

Here are the solutions with and without continuations for comparison:

def copyFileCPS = using(new BufferedReader(new FileReader("test.txt"))) {
  reader => {
   using(new BufferedWriter(new FileWriter("test_copy.txt"))) {
      writer => {
        var line = reader.readLine
        var count = 0
        while (line != null) {
          count += 1
          writer.write(line)
          writer.newLine
          line = reader.readLine
        }
        count
      }
    }
  }
}

def copyFileDC = withResources {
  val reader = resource[BufferedReader,Int](new BufferedReader(new FileReader("test.txt")))
  val writer = resource[BufferedWriter,Int](new BufferedWriter(new FileWriter("test_copy.txt")))
  var line = reader.readLine
  var count = 0
  while(line != null) {
    count += 1
    writer write line
    writer.newLine
    line = reader.readLine
  }
  count
}

And here's Tiark Rompf's suggestion of improvement:

trait ContextType[B]
def forceContextType[B]: ContextType[B] = null

// A DC version of 'using'
def resource[X <: {def close()}, B: ContextType](res : X): X @cps[B,B] = shift(using[X, B](res))

// some sugar for reset
def withResources[A](x : => A @cps[A, A]) = reset{x}

// and now use our new lib
def copyFileDC = withResources {
 implicit val _ = forceContextType[Int]
 val reader = resource(new BufferedReader(new FileReader("test.txt")))
 val writer = resource(new BufferedWriter(new FileWriter("test_copy.txt")))
 var line = reader.readLine
 var count = 0
 while(line != null) {
   count += 1
   writer write line
   writer.newLine
   line = reader.readLine
 }
 count
}
Daniel
+7  A: 

Daniel,

I've just recently deployed the scala-arm library for automatic resource management. You can find the documentation here: http://wiki.github.com/jsuereth/scala-arm/

This library supports three styles of usage (currently):

1) Imperative/for-expression:

import resource._
for(input <- managed(new FileInputStream("test.txt")) {
// Code that uses the input as a FileInputStream
}

2) Monadic-style

import resource._
import java.io._
val lines = for { input <- managed(new FileInputStream("test.txt"))
                  val bufferedReader = new BufferedReader(new InputStreamReader(input)) 
                  line <- makeBufferedReaderLineIterator(bufferedReader)
                } yield line.trim()
lines foreach println

3) Delimited Continuations-style

Here's an "echo" tcp server:

import java.io._
import util.continuations._
import resource._
def each_line_from(r : BufferedReader) : String @suspendable =
  shift { k =>
    var line = r.readLine
    while(line != null) {
      k(line)
      line = r.readLine
    }
  }
reset {
  val server = managed(new ServerSocket(8007)) !
  while(true) {
    // This reset is not needed, however the  below denotes a "flow" of execution that can be deferred.
    // One can envision an asynchronous execuction model that would support the exact same semantics as below.
    reset {
      val connection = managed(server.accept) !
      val output = managed(connection.getOutputStream) !
      val input = managed(connection.getInputStream) !
      val writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(output)))
      val reader = new BufferedReader(new InputStreamReader(input))
      writer.println(each_line_from(reader))
      writer.flush()
    }
  }
}

The code makes uses of a Resource type-trait, so it's able to adapt to most resource types. It has a fallback to use structural typing against classes with either a close or dispose method. Please check out the documentation and let me know if you think of any handy features to add.

jsuereth
Yes, I saw this. I want to look over the code, to see how you accomplish some things, but I'm way too busy right now. Anyway, since the goal of the question is to provide a reference to reliable ARM code, I'm making this the accepted answer.
Daniel