Skip to content

Instantly share code, notes, and snippets.

@torao
Created July 15, 2013 14:24
Show Gist options
  • Save torao/6000361 to your computer and use it in GitHub Desktop.
Save torao/6000361 to your computer and use it in GitHub Desktop.
Java のリソース構文や C# の using 節に似た Scala 版の何らかのもの。型パラメータ U は U >: java.io.Closeable の方が良いのかもしれないけど java.sql.Connection などを考えると structual subtyping の方が汎用的なのかなぁと。
// ex.,
// using(new FileInputStream("foo.txt")){ in => ... }
// using(DriverManager.getConnection("jdbc:db://localhost/foo")){ con => ... }
def using[T,U <% { def close():Unit }](c:U)(f:(U)=>T):T = try { f(c) } finally { c.close() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment