Skip to content

Instantly share code, notes, and snippets.

@orangy
Created May 2, 2014 12:56
Show Gist options
  • Save orangy/11474248 to your computer and use it in GitHub Desktop.
Save orangy/11474248 to your computer and use it in GitHub Desktop.
DSL for C#-style using statement
trait Disposable {
fun dispose()
}
fun using(value : Disposable, body : ()->Unit) {
try {
body()
} finally {
value.dispose()
}
}
fun fn() {
val resource = object : Disposable {
override fun dispose() {
println("disposed!")
}
}
using(resource) {
println("using resource")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment