Skip to content

Instantly share code, notes, and snippets.

@coder36
Created November 19, 2014 14:30
Show Gist options
  • Save coder36/bf4ca14cf9f1f411cdbe to your computer and use it in GitHub Desktop.
Save coder36/bf4ca14cf9f1f411cdbe to your computer and use it in GitHub Desktop.
Scala 'using' implementation
object UsingDemo {
def main( args : Array[String] ) {
using( new A() ) { a =>
a.print("Hello World " )
}
}
def using[A, B <: {def close(): Unit}] ( closeable:B ) ( f: B => A ) : A = {
try {
f(closeable)
}
finally {
closeable.close()
}
}
class A {
def print(s:String) = {
println( s )
}
def close() = {
println( "closing" )
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment