Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 7 style automatic resource management for Scala

Tags:

java

scala

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(...) { }?

like image 558
Ralph Avatar asked Sep 16 '11 11:09

Ralph


People also ask

Which one of the option is valid to close the resources automatically in Java 7?

automatic resource management or try-with-resources is a new exception handling mechanism that was introduced in Java 7, which automatically closes the resources used within the try-catch block.

Does Scala have try with resource construct built into it?

Overview. In Java, we can use the try-with-resources statement to automatically close all resources that we need in our code. This statement works with every class that implements the AutoCloseable interface. Unfortunately, such a feature does not exist in Scala.

Which of the following block can close clean up the resources automatically?

close() method on all resources initialized in the try block. Important Points: The finally blocks were used to clean up the resources before Java 7. After java 7, resource cleanup is done automatically.

How try with resource works internally?

The try -with-resources statement is a try statement that declares one or more resources. A resource is an object that must be closed after the program is finished with it. The try -with-resources statement ensures that each resource is closed at the end of the statement.


3 Answers

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

like image 162
Fabian Avatar answered Oct 17 '22 21:10

Fabian


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

like image 28
Alois Cochard Avatar answered Oct 17 '22 21:10

Alois Cochard


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.

like image 2
paradigmatic Avatar answered Oct 17 '22 20:10

paradigmatic