I want to write a generic helper method:
def using(closeable: [B has close() method], callback: [B has close() method] => A): A {
try {
callback(closeable)
} finally {
closeable.close()
}
}
with the intent being that I can use this with anything that has a close() method:
using(new FileInputStream(...)) {
stream => stream.read()
}
using(dataSource.getConnection) {
conn => using(conn.createStatement()) {
statement => using(statement.executeQuery("...")) {
rs => rs.getString(1)
}
}
}
What I’m looking for is how this is named, such that I could have searched for the syntax myself, and the syntax itself.
Debilski is right, the syntax for that would be
closable: { def close() }Edit: Here’s a link to an alternative Scala implementation of the .NET like using construct you want to use based on an object and it’s apply method.