Skip to content

Instantly share code, notes, and snippets.

@tobyweston
Created July 21, 2015 16:51
Show Gist options
  • Save tobyweston/0fbb8eb114db48596e6b to your computer and use it in GitHub Desktop.
Save tobyweston/0fbb8eb114db48596e6b to your computer and use it in GitHub Desktop.
Java 8 to Scala bridge methods
package bad.robot.radiate
import java.util.concurrent.Callable
import java.util.function.{Consumer, Supplier}
object FunctionInterfaceOps {
implicit def toConsumer[A](function: A => Unit): Consumer[A] = new Consumer[A]() {
override def accept(arg: A): Unit = function.apply(arg)
}
implicit def toSupplier[A](function: => A): Supplier[A] = new Supplier[A]() {
override def get(): A = function
}
implicit def toRunnable(function: => Unit): Runnable = new Runnable {
override def run(): Unit = function
}
implicit def toCallable[A](function: => A): Callable[A] = new Callable[A] {
override def call(): A = function
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment