Created
March 12, 2013 20:55
-
-
Save piratus/5146935 to your computer and use it in GitHub Desktop.
Just being impressed with Scala's DSL creation abilities
This file contains hidden or 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
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