11

Java 7 has introduced automatic resource management:

try (BufferedReader br = new BufferedReader(new FileReader(path))) {
  return br.readLine();
}

This will work with any class that implements java.lang.AutoClosable.

I know there are several examples of doing automatic resource management in Scala, including one demonstrated by Martin Odersky.

Is there any plan to add a language-level resource management to Scala, similar to Java's try(...) { }?

Charles
  • 50,943
  • 13
  • 104
  • 142
Ralph
  • 31,584
  • 38
  • 145
  • 282
  • I think the [scala-language] mailing list would be a better place to ask this question. – missingfaktor Sep 16 '11 at 11:30
  • 2
    I think if it was added, then not as a language feature but as part of the std lib. – ziggystar Sep 16 '11 at 11:31
  • There won't be any language level feature added as it's easily doable using the existing language features, as per the answers below. – sourcedelica Sep 16 '11 at 11:52
  • I guess I should amend my question to ask if there is any plan to add the pattern to the standard library. It really does not matter to me whether it is a part of the language or standard library, as long as it's available without using an outside resource. It seems silly to write a 1-page utility that has 37 Maven dependencies :-). – Ralph Sep 16 '11 at 12:50
  • I should really start using that. I missed C#'s Disposable pattern for ages now. – ripper234 Nov 20 '11 at 23:40
  • Because I need to be able to nest multiple java.lang.AutoCloseable instances, each of which depends upon the prior one successfully instantiating, I finally hit upon a pattern that has been very useful for me. I wrote it up as an answer on similar StackOverflow question: http://stackoverflow.com/a/34277491/501113 – chaotic3quilibrium Jan 22 '16 at 20:02

4 Answers4

13

In scala this can be added as a library. As a example scala-arm (https://github.com/jsuereth/scala-arm) from jsuereth:

Imperative Style:

// Copy input into output.
for(input <- managed(new java.io.FileInputStream("test.txt"); 
    output <- managed(new java.io.FileOutputStream("test2.txt")) {
  val buffer = new Array[Byte](512)
  while(input.read(buffer) != -1) {
    output.write(buffer);
  }
}

Monadic style

val first_ten_bytes = managed(new FileInputStream("test.txt")) map { 
   input =>
     val buffer = new Array[Byte](10)
     input.read(buffer)
     buffer
}

On the github page are some more examples

cdmckay
  • 31,832
  • 25
  • 83
  • 114
Fabian
  • 1,224
  • 1
  • 11
  • 26
3

I'm not aware of any Trait specially designed for that in Scala, but here is an example using the loan pattern on Java Closable:

http://whileonefork.blogspot.com/2011/03/c-using-is-loan-pattern-in-scala.html

EDIT

You can even make a more generic loaner by doing something like:

https://stackoverflow.com/questions/5945904/what-are-your-most-useful-own-library-extensions/5946514#5946514

Community
  • 1
  • 1
Alois Cochard
  • 9,812
  • 2
  • 29
  • 30
2

Scala specs are pretty thin, because almost everything that can be implemented via the standard library, is. Thus there is no real need of adding ARM in the language itself.

Until now, Scala as no real IO API (defaulting on Java IO API). It's probable that a future Scala IO API will include some form of ARM. For instance, scala-io has ARM.

paradigmatic
  • 40,153
  • 18
  • 88
  • 147
0

There is light-weight (10 lines of code) ARM included with better-files. See: https://github.com/pathikrit/better-files#lightweight-arm

import better.files._
for {
  in <- inputStream.autoClosed
  out <- outputStream.autoClosed
} in.pipeTo(out)
// The input and output streams are auto-closed once out of scope
pathikrit
  • 32,469
  • 37
  • 142
  • 221