Last active
August 29, 2015 13:57
-
-
Save manjuraj/9496590 to your computer and use it in GitHub Desktop.
implicit views
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
scala> :pa | |
// Entering paste mode (ctrl-D to finish) | |
trait Run { | |
def run(): Unit | |
} | |
def doRun(r: Run) = r.run() | |
// Exiting paste mode, now interpreting. | |
defined trait Run | |
doRun: (r: Run)Unit | |
scala> doRun(println("hello world")) | |
<console>:10: error: type mismatch; | |
found : Unit | |
required: Run | |
doRun(println("hello world")) | |
^ | |
scala> :pa | |
// Entering paste mode (ctrl-D to finish) | |
implicit def blockToRun(b: => Unit) = new Run { | |
def run() = b | |
} | |
// Exiting paste mode, now interpreting. | |
warning: there were 1 feature warning(s); re-run with -feature for details | |
blockToRun: (b: => Unit)Run | |
scala> doRun(println("hello world")) | |
hello world | |
scala> doRun(() => println("hello world")) | |
<console>:11: error: type mismatch; | |
found : () => Unit | |
required: Run | |
doRun(() => println("hello world")) | |
^ | |
scala> :pa | |
// Entering paste mode (ctrl-D to finish) | |
implicit def funcToRun(f: () => Unit) = new Run { | |
def run() = f() | |
} | |
// Exiting paste mode, now interpreting. | |
warning: there were 1 feature warning(s); re-run with -feature for details | |
funcToRun: (f: () => Unit)Run | |
scala> doRun(() => println("hello world")) | |
hello world |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment