Created
July 23, 2011 18:19
-
-
Save stephenh/1101709 to your computer and use it in GitHub Desktop.
handlers eagerly invoked
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
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