Created
July 15, 2013 14:24
-
-
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 の方が汎用的なのかなぁと。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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