Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scala ARM
import scala.language.reflectiveCalls
def using[TResource <: { def close(): Unit }, TResult](resource: => TResource)(action: TResource => TResult): TResult = {
val instance = resource
try {
action(instance)
} finally {
instance.close()
}
}
def manage[TResource <: { def close(): Unit }](resource: => TResource): Traversable[TResource] = new Traversable[TResource] {
override def foreach[TResult](action: TResource => TResult): Unit = {
val instance = resource
try {
action(instance)
} finally {
instance.close()
}
}
}
class Resource {
scala.Console.out.println("Opening...")
def close() {
scala.Console.out.println("Closing...")
}
}
using (new Resource) { r =>
}
for (r <- manage(new Resource)) {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.