Created
October 5, 2016 17:25
-
-
Save jehugaleahsa/396f5eddc73b23e3c34720a06b8f02bc to your computer and use it in GitHub Desktop.
Scala ARM
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
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