Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Daniel,</p> <p>I've just recently deployed the scala-arm library for automatic resource management. You can find the documentation here: <a href="http://wiki.github.com/jsuereth/scala-arm/" rel="noreferrer">http://wiki.github.com/jsuereth/scala-arm/</a></p> <p>This library supports three styles of usage (currently):</p> <p>1) Imperative/for-expression:</p> <pre><code>import resource._ for(input &lt;- managed(new FileInputStream("test.txt")) { // Code that uses the input as a FileInputStream } </code></pre> <p>2) Monadic-style</p> <pre><code>import resource._ import java.io._ val lines = for { input &lt;- managed(new FileInputStream("test.txt")) val bufferedReader = new BufferedReader(new InputStreamReader(input)) line &lt;- makeBufferedReaderLineIterator(bufferedReader) } yield line.trim() lines foreach println </code></pre> <p>3) Delimited Continuations-style</p> <p>Here's an "echo" tcp server:</p> <pre><code>import java.io._ import util.continuations._ import resource._ def each_line_from(r : BufferedReader) : String @suspendable = shift { k =&gt; 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() } } } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload