Skip to content

Instantly share code, notes, and snippets.

@piratus
Created March 12, 2013 20:55
Show Gist options
  • Save piratus/5146935 to your computer and use it in GitHub Desktop.
Save piratus/5146935 to your computer and use it in GitHub Desktop.
Just being impressed with Scala's DSL creation abilities
object With {
def apply[T <: { def enter(): T; def exit(): Unit}](obj:T)(block: (T) => Unit) {
obj.enter()
block(obj)
obj.exit()
}
}
class SomeClass {
def enter() = { println("enter"); this }
def exit() = { println("exit") }
override def toString = "SomeClass instance"
}
With(new SomeClass()) { obj =>
println(obj)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment