title: Simple Scala pattern for "using/try-with-resources" (Automatic Resource Management)
body: <p>C# has <code>using</code> with the <code>IDisposable</code> interface. Java 7+ has identical functionality with <code>try</code> and the <code>AutoCloseable</code> interface. Scala lets you choose your own implementation to this issue.</p>
  
  <p>scala-arm seems to be the popular choice, and is maintained by one of the Typesafe employees. However, it seems very complicated for such a simple behavior. To clarify, the usage instructions are simple, but understanding how all that code is working internally is rather complex.</p>
  
  <p>I just wrote the following super simple ARM solution:</p>
  
  <pre><code>object SimpleARM {
    def apply[T, Q](c: T {def close(): Unit})(f: (T) =&gt; Q): Q = {
      try {
        f(c)
      } finally {
        c.close()
      }
    }
  }
  </code></pre>
  
  <ul>
  <li>Is there any benefit to something like simple-arm? It seems all the extra complexity should deliver extra benefit.</li>
  <li>Normally, it is highly preferable to use a public, open source, library that is supported by others for general purpose behavior over using custom code.</li>
  <li>Can anyone recommend any improvements?</li>
  <li>Are there any limitations to this simple approach?</li>
  </ul>