Skip to content

Instantly share code, notes, and snippets.

@manjuraj
Last active August 29, 2015 13:57
Show Gist options
  • Save manjuraj/9496590 to your computer and use it in GitHub Desktop.
Save manjuraj/9496590 to your computer and use it in GitHub Desktop.
implicit views
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