Created
July 6, 2011 21:07
-
-
Save ahoy-jon/1068327 to your computer and use it in GitHub Desktop.
ça gère bien les ressources Scala
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
object Control { | |
def using[Closeable <: {def close(): Unit}, B](closeable: Closeable)(useCloseable: Closeable => B): B = | |
try { | |
useCloseable(closeable) | |
} finally { | |
closeable.close() | |
} | |
} | |
class TestSql { | |
import java.sql._ | |
import Control._ | |
def query[B](connection: Connection, sql: String)(process: ResultSet => B): B = | |
using (connection) { connection => | |
using (connection.createStatement) { statement => | |
using (statement.executeQuery(sql)) { results => | |
process(results) | |
} | |
} | |
} | |
} | |
class TestReader { | |
import java.io._; | |
import Control._ | |
def run() : Unit = | |
using(new FileReader("FileReaderDemo.java")) { | |
reader => val br = new BufferedReader(reader); | |
var s:String = null ; | |
// the following is boring... you don't read files this way in scala. | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Pour les effets de bord ... on est déjà sur du pur IO là ;)