Skip to content

Instantly share code, notes, and snippets.

@stephenh
Created July 23, 2011 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenh/1101709 to your computer and use it in GitHub Desktop.
Save stephenh/1101709 to your computer and use it in GitHub Desktop.
handlers eagerly invoked
package foo
trait SAM {
def onSam(): Unit
}
object Foo {
def main(args: Array[String]): Unit = {
doFunction { println("1"); println("2") }
// got <function0>
// 1 2 1 2
doSam(body2sam({ println("1"); println("2") }))
// got foo.Foo$$anon$1@2b86c6b2
// 1 2 1 2
doSam { println("1"); println("2") }
// 1
// got foo.Foo$$anon$1@416b13c7
// 2 2
}
def doSam(sam: SAM) = { println("got " + sam) ; sam.onSam() ; sam.onSam() }
def doFunction(f: => Unit) = { println("got " + (f _)) ; f ; f }
implicit def body2sam(f: => Unit): SAM = new SAM {
override def onSam = f
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment